Hi rhino000,
I checked your previous question where you asked for dynamically column binding to other GridView. From same sample code i made change so you can find index as dynamically and check for same row column value by dynamically index with related column value if it exist in dynamic grid. Your design code is same as per previous question. Refer below code and implement it by your logic.
Just modified one method (Getsorting) Data and one event (cckk_SelectedIndexChanged) from previous code also added the new Save event which you have to modify by your self-according to your code.
protected void cckk_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> columnName = null;
/*To get currently checked or unchecked Item from checkboxList*/
string eventTarget = Request.Form.Get("__EVENTTARGET");
string s = eventTarget.Split('$')[1];
int index = Convert.ToInt32(eventTarget.Substring(eventTarget.Length - 1));
bool selected = cckk.Items[index].Selected;
// To find total unchecked items from checkbox
List<ListItem> UncheckedItems = cckk.Items.Cast<ListItem>()
.Where(li => !li.Selected)
.ToList();
// to find selected Item text for column name
string value = cckk.Items[index].Text;
// if ViewState["SortData"] null then it will add first column dynamically
if (ViewState["SortData"] == null)
{
DataTable dt = new DataTable();
columnName = new List<string>();
columnName.Add(value);
ViewState["SortData"] = columnName;
}
else
{
// if ViewState["SortData"] not null it will get latest column strucher
columnName = (List<string>)ViewState["SortData"];
// If item checked then add column to column Name string
if (selected)
{
columnName.Add(value);
}
else
{
// If item unchecked then remove column to column Name string
columnName.Remove(value);
// if total unchecked Items equal to Total checkboxlist item then it will make ViewState["SortData"] as null
if (UncheckedItems.Count == cckk.Items.Count)
{
ViewState["SortData"] = null;
}
}
}
GetsortingData(columnName);
}
private void GetsortingData(List<string> columnName)
{
DataTable dtsorting = new DataTable();
dtsorting = (DataTable)ViewState["Data"];
DataTable dummaydt = new DataTable();
// Added viewstate which will also hold the data of dynamically created gridview data
ViewState["DynamicData"] = null;
if (columnName.Count > 0)
{
dummaydt = dtsorting.DefaultView.ToTable(false, columnName.ToArray());
gridview1.DataSource = dummaydt;
gridview1.DataBind();
// Onces data bind set view state as null
ViewState["DynamicData"] = dummaydt;
}
else
{
gridview1.DataSource = null;
gridview1.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
if (ViewState["DynamicData"] != null)
{
// Parsed Viewstate with DataTable
DataTable dt = (DataTable)ViewState["DynamicData"];
// Created List As string which will hold the column name by the order which dynamic grid contain
List<string> columnName = new List<string>();
for (int i = 0; i < dt.Columns.Count; i++)
{
// Add column name to string from Datatable column name
string columnname1 = dt.Columns[i].ToString();
columnName.Add(columnname1);
}
// Find the index from the columnName List<string> If column name exist it will give current index else it will return -1
int customerIdIndex = columnName.FindIndex(city => city.ToLower() == "customerid");
int cityIndex = columnName.FindIndex(city => city.ToLower() == "city");
int countryIndex = columnName.FindIndex(city => city.ToLower() == "country");
int postalCodeIndex = columnName.FindIndex(city => city.ToLower() == "postalcode");
foreach (GridViewRow row in gridview1.Rows)
{
// Here you can check value by dynmaically sets index from column name
// its on you that if you not selected the column name then you have to set value for string as empty or null
string customerId = customerIdIndex != -1 ? row.Cells[customerIdIndex].Text : string.Empty;
string city = cityIndex != -1 ? row.Cells[cityIndex].Text : string.Empty;
string country = countryIndex != -1 ? row.Cells[countryIndex].Text : string.Empty;
string postalCode = postalCodeIndex != -1 ? row.Cells[postalCodeIndex].Text : string.Empty;
// rest your code where you can Save as per your logic
}
}
}
ScreenShoot
