简体   繁体   中英

How to edit the calendar control in a gridview?

I tried to update the data in edit mode of gridview.

It does not send correctly with my actual code. I tried to make it a templateField in HTML and it does not work. At this moment,the program break when I modify the date in the edit mode of gridview. This is put in Page_Load.In the condition to sort the grid, the grid is binding.

     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" />

To edit the date into gridview and update in the database.

Your MyDateInsCalendar variable is an objcect (Calendar). Use one of the properties of this variable in your SQL statement.

for example:

<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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM