Hi
In a Gridview there r 5 columns . 1 column is Date which is the last column.
I want to make that column only editatble & user should enter date in dd-mm-yyyy format only otherwise it should give message
I have tried this but it is not making editable
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;
int lastColumnIndex = BookPlanningResult.Columns.Count - 1;
BookPlanningResult.Columns[lastColumnIndex].ReadOnly = false;
BookPlanningResult.Columns[lastColumnIndex-1].ReadOnly = false;
grdPlanning.DataBind();
}Thanks

Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 29, 2023, 11:47 AM EDT
foreach (DataControlField column in grdPlanning.Columns) { column.ReadOnly = true; } grdPlanning.Columns[grdPlanning.Columns.Count - 1].ReadOnly = false; // set the last column (Date) to be editableprotected void grdPlanning_RowEditing(object sender, GridViewEditEventArgs e) { // Get the cell that contains the Date column for the selected row TableCell dateCell = grdPlanning.Rows[e.NewEditIndex].Cells[grdPlanning.Columns.Count - 1]; // Make the cell editable dateCell.ReadOnly = false; // Set the input type of the cell to "text" to allow the user to enter a date dateCell.Attributes.Add("type", "text"); // Set the format of the date input to "dd-mm-yyyy" using HTML5 "pattern" attribute dateCell.Attributes.Add("pattern", "\\d{2}-\\d{2}-\\d{4}"); // Set the placeholder text to show the expected date format dateCell.Attributes.Add("placeholder", "dd-mm-yyyy"); }protected void grdPlanning_RowUpdating(object sender, GridViewUpdateEventArgs e) { // Get the new value entered by the user for the Date column string newDate = e.NewValues[e.NewValues.Count - 1].ToString(); // Validate the date format using regular expressions Regex dateFormat = new Regex(@"^\d{2}-\d{2}-\d{4}$"); if (!dateFormat.IsMatch(newDate)) { // Cancel the update and show an error message e.Cancel = true; ScriptManager.RegisterStartupScript(this, GetType(), "InvalidDate", "alert('Please enter the date in dd-mm-yyyy format.');", true); } }Ramco RamcoRank #462 · 5.4K reputationPosted Apr 29, 2023, 1:09 PM EDT
Hi
It is giving error 'DataControlField does nor contain a definition for ReadOnly
grdPlanning.DataSource = BookPlanningResult; foreach (DataControlField column in grdPlanning.Columns) { column.ReadOnly = true; }Thanks