Hi
I have below code & i want to loop thru Gridview which have value in Date column i want to get Remarks column value also stored in variable
C#
DataTable dt = sQLUtility.GeBookPlanningRecommendation(HRpl, BookCollection, StudentCollection).Tables[0];
if (dt != null)
{
DataColumn Remarks = new DataColumn("Remarks", typeof(string));
dt.Columns.Add(Remarks);
DataColumn Dates = new DataColumn("Date", typeof(DateTime));
dt.Columns.Add(Dates);
grdPlanning.DataSource = dt;
grdPlanning.DataBind();
}
Rajkiran SwainRank #165 · 14.4K reputationPosted May 4, 2023, 7:39 AM EDT
foreach (GridViewRow row in grdPlanning.Rows) { DateTime date = DateTime.Parse(row.Cells[1].Text); string remarks = ((TextBox)row.Cells[2].FindControl("txtRemarks")).Text; // Do something with the date and remarks values }Tuhin PaulRank #7 · 71.9K reputationPosted May 4, 2023, 8:08 AM EDT
To loop through the GridView and get the Remarks column value stored in a variable, you can use the following code:
foreach (GridViewRow row in grdPlanning.Rows) { if (!string.IsNullOrEmpty(row.Cells[1].Text) && DateTime.TryParse(row.Cells[1].Text, out DateTime dateValue)) { string remarks = row.Cells[2].Text; // do something with the remarks value } }In this code, the `foreach` loop is used to iterate through each row in the GridView. The condition checks if the second column (index 1) contains a valid date value. If it does, the `remarks` variable is assigned the value of the third column (index 2), which is the Remarks column. You can then perform any desired operations on the `remarks` variable.
Tuhin PaulRank #7 · 71.9K reputationPosted May 4, 2023, 8:10 AM EDT
Here is the code code that loops through the GridView and retrieves the Remarks column value for each row that has a value in the Date column.
// Get the data table from the SQL query DataTable dt = sQLUtility.GeBookPlanningRecommendation(HRpl, BookCollection, StudentCollection).Tables[0]; // Add the Remarks and Dates columns to the data table dt.Columns.Add("Remarks", typeof(string)); dt.Columns.Add("Date", typeof(DateTime)); // Bind the data table to the GridView grdPlanning.DataSource = dt; grdPlanning.DataBind(); // Loop through the GridView rows and retrieve the Remarks column value for each row that has a value in the Date column List remarksList = new List(); foreach (GridViewRow row in grdPlanning.Rows) { DateTime dateValue; if (DateTime.TryParse(row.Cells[1].Text, out dateValue)) { string remarksValue = row.Cells[2].Text; remarksList.Add(remarksValue); } } // Store the remarks values in a string variable string remarks = string.Join(", ", remarksList);We first add the Remarks and Dates columns to the data table before binding it to the GridView. Then, we loop through the GridView rows and retrieve the Remarks column value for each row that has a value in the Date column. We store the remarks values in a string variable.