1

私の意図は、intで表される月の値から切り替えることでした(データベーステーブルにあるように)

それを文字列(月の名前)として(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;

この単純なタスクを達成するために、エラーはほとんどありませんでしたが、それを達成するための正しい方法の例を示したいと思います。私はそれがintを介して表示する単純な原因だと思いました

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

「うまくいく」

しかし、Bind( "columnName")で試してみると、初心者としては複雑すぎました。

値が実際にはCells[0]にあるのに、Cells[1]の中にあると仮定して間違っていました。

だから私はそれを通常モードと編集モードで持っていますが、編集可能ではありませんが、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** ?
4

2 に答える 2

2

これは、int から文字列の月名を取得するために行うことです。

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);

詳細はこちら:

c#で整数を月名に変換する最良の方法は?

SQLサーバーで月番号を月名に変換するには、次を参照してください。

SQL で月番号を月名関数に変換する

- - 編集

OK、RowDataBound では、int を string に変換したいので、次のようになります。

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 で、月名を再度 int に変換してから、データセットを更新する (またはデータベースに保存する) 必要があります。

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();
}
于 2012-09-17T08:30:19.723 に答える
1

ページクラス:

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

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

グリッドビュー:

    <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: value="1" Text="תשרי" などを含む DropDownList があります。これにより、編集のためにデータ レイヤーに渡される月数になります。

于 2012-09-17T08:48:18.057 に答える