Hi
In below Gridview i want last column Date to be Editable
Code
C#
DataTable BookPlanningResult = sQLUtility.GeBookPlanningRecommendation(vRpl, BookCollection, StudentCollection).Tables[0];
if (BookPlanningResult != null)
{
DataColumn Remarks = new DataColumn("Remarks", typeof(string));
BookPlanningResult.Columns.Add(Remarks);
DataColumn Dates = new DataColumn("Date", typeof(DateTime));
BookPlanningResult.Columns.Add(Dates);
grdPlanning.DataSource = BookPlanningResult;
grdPlanning.DataBind();
}Thanks

Amit MohantyRank #112 · 20K reputationPosted May 1, 2023, 12:34 AM EDT
To make the last column "Date" editable in the GridView, you can add a TemplateField column to the GridView's Columns collection.
'> '>You also need to set its "EditIndex" property to the index of the row being edited, either in code-behind or in response to a user action.
protected void grdPlanning_RowEditing(object sender, GridViewEditEventArgs e) { grdPlanning.EditIndex = e.NewEditIndex; BindGridView(); // Rebind the GridView to display the editable row }Naimish MakwanaRank #324 · 7.7K reputationPosted May 1, 2023, 12:55 AM EDT
To make the last column "Date" editable in the GridView, you need to add an Edit button to each row and handle its click event to enable editing mode for the row. You can use the following code to achieve this:
1.Add a TemplateField to the GridView for the Edit button and set its CommandName to "Edit":
2.Handle the RowCommand event of the GridView and enable editing mode for the row if the CommandName is "Edit":
protected void grdPlanning_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Edit") { int rowIndex = Convert.ToInt32(e.CommandArgument); grdPlanning.EditIndex = rowIndex; BindGridViewData(); } }3. Bind the GridView data and set the TextBox control for the "Date" column to be editable when the row is in edit mode:
protected void BindGridViewData() { DataTable BookPlanningResult = sQLUtility.GeBookPlanningRecommendation(vRpl, BookCollection, StudentCollection).Tables[0]; if (BookPlanningResult != null) { DataColumn Remarks = new DataColumn("Remarks", typeof(string)); BookPlanningResult.Columns.Add(Remarks); DataColumn Dates = new DataColumn("Date", typeof(DateTime)); BookPlanningResult.Columns.Add(Dates); grdPlanning.DataSource = BookPlanningResult; grdPlanning.DataBind(); // set the "Date" column to be editable in edit mode foreach (GridViewRow row in grdPlanning.Rows) { TextBox txtDate = row.FindControl("txtDate") as TextBox; if (txtDate != null) { if (row.RowIndex == grdPlanning.EditIndex) { txtDate.Enabled = true; } else { txtDate.Enabled = false; } } } } }4. Finally, add a TextBox control to the ItemTemplate and EditItemTemplate of the "Date" column:
'> '>Thanks