Hi
I have below code & i want to make Date Column editable.
C#
SQLUtility sQLUtility = new SQLUtility();
DataTable BookPlanningResult = sQLUtility.GeBookPlanningRecommendation(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

Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 26, 2023, 6:43 AM EDT
Assuming that you are using a GridView control to display the data, you can make the "Date" column editable by setting the "ReadOnly" property of the corresponding column in the GridView to "false". Here's how you can modify the code:
SQLUtility sQLUtility = new SQLUtility(); DataTable BookPlanningResult = sQLUtility.GeBookPlanningRecommendation(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.Columns["Date"].ReadOnly = false; // Set the ReadOnly property to false for the "Date" column grdPlanning.DataBind(); }Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 26, 2023, 6:56 AM EDT
Hi
It is gicing error - Cannot convert from string to int
grdPlanning.Columns["Date"].ReadOnly = false;
Naimish , second option can be How to add edit button & in Modal Popup user should fill Date & it should reflect in that row Date column.
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 26, 2023, 7:00 AM EDT
To make the "Date" column editable, you can either use the index of the column in the collection or find the column by name using the LINQ extension method "FirstOrDefault()". Here are two possible ways to modify the code:
// Option 1: use the column index grdPlanning.Columns[1].ReadOnly = false; // assuming that "Date" is the second column in the GridView
// Option 2: use LINQ to find the column by name var dateColumn = grdPlanning.Columns.Cast().FirstOrDefault(col => col.Name == "Date"); if (dateColumn != null) { dateColumn.ReadOnly = false; }
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 26, 2023, 7:02 AM EDT
Hi naimish
Columns will be dynamic. But it will be the Last Column
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 26, 2023, 7:05 AM EDT
If the "Date" column is always the last column in the GridView, you can use the "Columns.Count" property to get the index of the last column and make it editable. Here's how you can modify the code to achieve this:
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;
int lastColumnIndex = grdPlanning.Columns.Count - 1;
grdPlanning.Columns[lastColumnIndex].ReadOnly = false; // Set the ReadOnly property to false for the last column
grdPlanning.DataBind();
}
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 26, 2023, 7:11 AM EDT
Hi
It is giving message 'DataControlField does not contain a definition for ReadOnly.
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 30, 2023, 10:38 PM EDT
Hi
DataControlField does not contain a definition for ReadOnly
int lastColumnIndex = BookPlanningResult.Columns.Count - 1;
grdPlanning.Columns[lastColumnIndex - 1].ReadOnly = false;
Thanks