2012-09-17 45 views
1

我的意圖是切換形成一個int-表示月份值 (因爲它是在數據庫表)gridview的列值操縱

轉換它(在GridView中顯示)作爲字符串(月份名稱) 並返回它以int形式返回數據庫(轉換回原始類型,以int表示的月份)。

這些都是在我的GridView中的相關要素,

<asp:GridView ID="GV_DaysPerMonth" runat="server" DataSourceID="dsWorkDayPerMonth" 
     AutoGenerateColumns="False" DataKeyNames="recordID" AllowPaging="True" 
     CellPadding="4" ForeColor="#333333" GridLines="None" Font-Names="arial" PageSize="12" 
     OnRowDataBound="GV_DaysPerMonth_RowDataBound" 
     OnRowEditing="GV_DaysPerMonth_RowEditing" 
     OnRowUpdating="GV_DaysPerMonth_RowUpdating"> 
    <AlternatingRowStyle BackColor="White" /> 
    <Columns> 



       <asp:TemplateField HeaderText="חודש" ControlStyle-Width="100" HeaderStyle-Width="120" ItemStyle-HorizontalAlign="Center"> 
        <ItemTemplate> 
         <%# Eval("theMonth")%> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="TBX_theMonth" runat="server" Text='<%# Bind("theMonth")%>' /> 
        </EditItemTemplate> 

<asp:GridView ID="GV_DaysPerMonth" runat="server" DataSourceID="dsWorkDayPerMonth" 
     AutoGenerateColumns="False" DataKeyNames="recordID" AllowPaging="True" 
     CellPadding="4" ForeColor="#333333" GridLines="None" Font-Names="arial" PageSize="12" 
     OnRowDataBound="GV_DaysPerMonth_RowDataBound" 
     OnRowEditing="GV_DaysPerMonth_RowEditing" 
     OnRowUpdating="GV_DaysPerMonth_RowUpdating"> 
    <AlternatingRowStyle BackColor="White" /> 
    <Columns> 



       <asp:TemplateField HeaderText="Current Month" ControlStyle-Width="100" HeaderStyle-Width="120" ItemStyle-HorizontalAlign="Center"> 
        <ItemTemplate> 
         <%# Eval("theMonth")%> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="TBX_theMonth" runat="server" Text='<%# Bind("theMonth")%>' /> 
        </EditItemTemplate> 

我使用這些輔助方法從代碼

public static CultureInfo ILci = CultureInfo.CreateSpecificCulture("he-IL"); 

    public static string GetMonthName(int mInt) 
    { 
     DateTime fullDate = new DateTime(2012, mInt, 2); 
     string[] tempDayArray = fullDate.ToString("MMMM", ILci).Split(' '); 
     return tempDayArray[0]; 
    } 
    public static int GetMonthAsInt(string mStr) 
    { 
     return DateTime.ParseExact(mStr, "MMMM", ILci).Month; 

背後來實現這一SIMPL特林e任務,但幾乎沒有錯誤,我想以一個例子來說明如何以正確的方式實現它。 我認爲這是簡單的因果顯示INT通過

<%# manipulation Function(Eval("columnName")) %> 

會「只是工作」

,但它與綁定(「列名」)試圖

當它變得過於複雜,我作爲福利局 我錯誤的假設值是在細胞[1]時,它實際上是在細胞[0]

所以我確實在正常模式下 也在編輯模式下雖然不可編輯但通過標籤視圖模式,而不是一個TextBox

protected void GV_DaysPerMonth_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    RowNum = GV_DaysPerMonth.Rows.Count; 
    GridViewRow CurRow = e.Row;  // Retrieve the current row. 

    if (CurRow.RowType == DataControlRowType.DataRow) 
    { 
     bool isntEmptyMonth = string.IsNullOrEmpty(e.Row.Cells[0].Text) == false; 
     if (isntEmptyMonth) 
     { 
      e.Row.Cells[1].Text = RobCS.RDates.GetMonthName(Convert.ToInt32((e.Row.Cells[0].Text))); 
     } 

    } 
} 


so i think it might be the **missing handler for the edit mode** ? 

回答

2

這是你會怎樣做才能從一個int得到一個字符串月份名稱:

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1); 

此處瞭解詳情:

Best way to turn an integer into a month name in c#?

對於將月份編號轉換爲sql server中的月份名稱,請看這裏:

Convert Month Number to Month Name Function in SQL

----編輯

OK,上的RowDataBound,你想轉換爲int到字符串,所以它會是這樣的:

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 

    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // Display the month name. When you GridView is bound for the first time, you 
     // will bind the month number, which you can get in e.Row.Cells[1].Text. If 
    // Cells[1] does not work, try Cells[2], till you get the correct value. The 
    // convert the int to month name and assign it to the same Cell. 
    e.Row.Cells[1].Text = GetMonthNameFromInt(e.Row.Cells[1].Text) 

    } 

} 

在Row_updating,你要轉換的月份名稱重新詮釋,然後更新您的數據集(或者保存到數據庫中)

protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{  

    //Update the values. 
    GridViewRow row = GridView.Rows[e.RowIndex]; 
    var monthName = ((TextBox)(row.Cells[1].Controls[0])).Text; 
    var monthNumber = GetMonthNumber(monthName); 

    // code to update your dataset or database with month number 

    //Reset the edit index. 
    GridView.EditIndex = -1; 

    //Bind data to the GridView control. 
    BindData(); 
} 
+0

感謝您的信息,雖然問題不是**如何maipulate **而是如何實現正確的方式發送回數據庫的時候編輯爲名稱需要返回爲int,因爲它本來是 – LoneXcoder

+0

爲什麼不能你在databind gridview之前將月份數字轉換爲月份名稱?從數據庫獲取數據時,可以將存儲過程本身的月份數轉換爲月份名稱,也可以在將數據集綁定到gridview之前使用C#進行。對於編輯,您可以捕獲GridView_onRowEdited事件,並使用DateTime.ParseExact(monthName,「MMMM」,CultureInfo.CurrentCulture)將該字符串轉換爲int。月份 – tranceporter

+0

這正是我遇到的困難:「陷阱GridView_onRowEdited事件,並將字符串轉換爲int使用DateTime.ParseExact'「我已經有一個事件,因爲我的語法缺乏知識,如果我只能有一個更新前的價值的例子,然後更新後的值。 – LoneXcoder

1

Page類:

private static CultureInfo culture = CultureInfo.CreateSpecificCulture("he-IL"); 

public static string GetMonthName(string monthNum) 
{ 
    return culture.DateTimeFormat.GetMonthName(Convert.ToInt32(monthNum)); 
} 

GridView控件:

<ItemTemplate> 
     <%# GetMonthName(Eval("theMonth").ToString())%> 
    </ItemTemplate> 
    <EditItemTemplate> 
     <asp:DropDownList ID="ddlMonth" runat="server" SelectedValue='<%# Bind("theMonth")%>'> 
      <asp:ListItem Value="1" Text="תשרי"></ListItem> 
       etc... 
     </asp:DropDownList> 
    </EditItemTemplate> 

在你EditTemplate有listItems中的一個DropDownList:值=「1」文本=「תשרי」等。因此,它是一個的傳遞給你的數據層進行編輯的月份數。

+0

試圖通過DDL避免使用硬編碼項目值,而使用'靈活'的方式,可以用於任何形式,也許它更好地使用硬編碼項目比文本框編輯字段 – LoneXcoder

+0

使用DropDownList是一個更好的主意,即文本框。 DDL會讓你控制字符串名稱(1月,2月)等,在文本框,你將不得不驗證用戶輸入,這是額外的頭痛。 – tranceporter

+0

如果您想使用它自己的DataSource,仍然可以綁定DropDownList。 –