【问题标题】:How to edit the calendar control in a gridview?如何在 gridview 中编辑日历控件?
【发布时间】:2019-05-22 13:20:15
【问题描述】:

我尝试在gridview的编辑模式下更新数据。

我的实际代码无法正确发送。我试图使它成为 HTML 中的 templateField,但它不起作用。此时,当我在gridview的编辑模式下修改日期时程序中断。 这是放在Page_Load中的。在对网格进行排序的条件下,网格是绑定的。

     if (ViewState["sorting"] == null)
    {

        String myquery = "Select * from Venituri";
        SqlConnection sqlCon = new SqlConnection(CS);
        SqlCommand cmd = new SqlCommand
        {
            CommandText = myquery,
            Connection = sqlCon
        };
        SqlDataAdapter da = new SqlDataAdapter
        {
            SelectCommand = cmd
        };
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridViewIncomes.DataSource = ds;
        GridViewIncomes.DataSourceID = String.Empty;
        GridViewIncomes.DataBind(); //here is a break when I was modified with the suggest code
    }




protected void GridViewIncomes_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlConnection sqlCon = new SqlConnection(CS);
    int index = GridViewIncomes.EditIndex;
    GridViewRow row = GridViewIncomes.Rows[index];
    int VenitId = Convert.ToInt32(GridViewIncomes.DataKeys[e.RowIndex].Value);
    string Denumire = ((TextBox)row.Cells[2].Controls[0]).Text.ToString().Trim();
    var MyDateInsCalendar = GridViewIncomes.Rows[GridViewIncomes.EditIndex].FindControl("Data") as Calendar;
    MyDateInsCalendar.Visible = false;
    string Suma = ((TextBox)row.Cells[4].Controls[0]).Text.ToString().Trim();
    string Descriere = ((TextBox)row.Cells[5].Controls[0]).Text.ToString().Trim();
    string sql = "UPDATE Venituri SET Denumire='" + Denumire + "',Data='" + MyDateInsCalendar + "',Suma='" + Suma + "',Descriere='" + Descriere + "' WHERE VenitId=" + VenitId + "";

    SqlCommand cmd = new SqlCommand(sql, sqlCon);
    sqlCon.Open();
    int temp = cmd.ExecuteNonQuery();
    sqlCon.Close();
    if (temp == 1)
    {

        lblSuccessMessage.Text = "Actualizat cu succes!";
    }
    GridViewIncomes.EditIndex = -1;
    lblSuccessMessage.Text = "";

}

`

.aspx <asp:BoundField HeaderText="Data" SortExpression="Data" DataField="Data" />

将日期编辑到gridview并在数据库中更新。

【问题讨论】:

    标签: c# gridview webforms calendar-control


    【解决方案1】:

    您的MyDateInsCalendar 变量是一个对象(日历)。在您的 SQL 语句中使用此变量的属性之一。

    例如:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="true" OnRowEditing="GridView1_RowEditing" AutoGenerateColumns="false" OnRowUpdating="GridView1_RowUpdating">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>                
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    .cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    
    private void BindGrid()
    {
        List<string> tmp = new List<string>();
        tmp.Add("a");
        GridView1.DataSource = tmp;
        GridView1.DataBind();
    }
    
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Calendar cal = GridView1.Rows[e.RowIndex].FindControl("Calendar1") as Calendar;
        string tmp = cal.SelectedDate.ToString();
        BindGrid();
    }
    

    【讨论】:

    • 它不工作。错误是:引用了一个空对象。我认为问题出在这一行: var MyDateInsCalendar = GridViewIncomes.Rows[GridViewIncomes.EditIndex].FindControl("Data") as Calendar;
    • 希望这个例子对你有所帮助
    • 我在上面编辑了我的代码。我不能完全使用你的建议,因为我有一个 SQLData Source 并且不能绑定两次。我只使用带有日历的部分。我收到以下错误:将 varchar 数据类型转换为 datetime 数据类型导致值超出范围。声明已终止。'
    • 您的 MyDateInsCalendar 变量是一个对象(日历)。在您的 SQL 语句中使用此变量的属性之一。例如:MyDateInsCalendar.SelectedDate.To.String()
    • 从您的代码中,我复制了 Grid_Updating 中的行;我在 SQL 语句中使用 'tmp',这是一样的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2014-03-02
    相关资源
    最近更新 更多