I had been looking for the solution been a month. I have surfed all the related post which seems similar but doesn’t match my requirement. Therefore I am posting this query which will sound similar to the old post that other friends might have posted, however it doesn’t meet my problem.
I am binding a data in Grid View (Name : GridView3) where I am using Bound Field to get the data, I have a checkbox in this Grid View and On Checked Changed I am creating a Data Table and passing the value of this Bound Field in new Grid View (Name: GridView4). I am using Template Field in GridView4 to bind the value passed from GridView3, but in GridView4 I have added one extra Template Field which is a Text Box which doesn’t get value from Gridview3, here I am typing the value manually as per my requirement. The problem is, the value which I am typing manually in Text Box get lost whenever I try to add new row in GridView4 from On Checked Changed event of GridView3. So, how do I stop losing the value of this particular Text Box on Post Back?
GridView3:
GridView4
Below is the code behind:
protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
pnl_InProduct.Visible = true;
GetData();
SetData();
BindSecondaryGrid();
}
private DataTable CreateDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("AId");
dt.Columns.Add("AssetsName");
dt.Columns.Add("SubAssetName");
dt.AcceptChanges();
return dt;
}
private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("AId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["AId"] = gvRow.Cells[1].Text;
dt.Rows[dt.Rows.Count - 1]["AssetsName"] = gvRow.Cells[2].Text;
dt.Rows[dt.Rows.Count - 1]["SubAssetName"] = gvRow.Cells[3].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("AId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
private void GetData()
{
DataTable dt;
if (ViewState["SelectedRecords"] != null)
dt = (DataTable)ViewState["SelectedRecords"];
else
dt = CreateDataTable();
CheckBox chkAll = (CheckBox)GridView3.HeaderRow.Cells[0].FindControl("chkAll");
for (int i = 0; i < GridView3.Rows.Count; i++)
{
if (chkAll.Checked)
{
dt = AddRow(GridView3.Rows[i], dt);
}
else
{
CheckBox chk = (CheckBox)GridView3.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
dt = AddRow(GridView3.Rows[i], dt);
}
else
{
dt = RemoveRow(GridView3.Rows[i], dt);
}
}
}
ViewState["SelectedRecords"] = dt;
}
private void SetData()
{
CheckBox chkAll = (CheckBox)GridView3.HeaderRow.Cells[0].FindControl("chkAll");
chkAll.Checked = true;
if (ViewState["SelectedRecords"] != null)
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
for (int i = 0; i < GridView3.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView3.Rows[i].Cells[0].FindControl("chk");
if (chk != null)
{
DataRow[] dr = dt.Select("AId = '" + GridView3.Rows[i].Cells[1].Text + "'");
chk.Checked = dr.Length > 0;
if (!chk.Checked)
{
chkAll.Checked = false;
}
}
}
}
}
private void BindSecondaryGrid()
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
GridView4.DataSource = dt;
GridView4.DataBind();
}
protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
pnl_InProduct.Visible = true;
GetData();
SetData();
BindSecondaryGrid();
}
private DataTable CreateDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("AId");
dt.Columns.Add("AssetsName");
dt.Columns.Add("SubAssetName");
dt.AcceptChanges();
return dt;
}
private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("AId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["AId"] = gvRow.Cells[1].Text;
dt.Rows[dt.Rows.Count - 1]["AssetsName"] = gvRow.Cells[2].Text;
dt.Rows[dt.Rows.Count - 1]["SubAssetName"] = gvRow.Cells[3].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("AId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
private void GetData()
{
DataTable dt;
if (ViewState["SelectedRecords"] != null)
dt = (DataTable)ViewState["SelectedRecords"];
else
dt = CreateDataTable();
CheckBox chkAll = (CheckBox)GridView3.HeaderRow.Cells[0].FindControl("chkAll");
for (int i = 0; i < GridView3.Rows.Count; i++)
{
if (chkAll.Checked)
{
dt = AddRow(GridView3.Rows[i], dt);
}
else
{
CheckBox chk = (CheckBox)GridView3.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
dt = AddRow(GridView3.Rows[i], dt);
}
else
{
dt = RemoveRow(GridView3.Rows[i], dt);
}
}
}
ViewState["SelectedRecords"] = dt;
}
private void SetData()
{
CheckBox chkAll = (CheckBox)GridView3.HeaderRow.Cells[0].FindControl("chkAll");
chkAll.Checked = true;
if (ViewState["SelectedRecords"] != null)
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
for (int i = 0; i < GridView3.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView3.Rows[i].Cells[0].FindControl("chk");
if (chk != null)
{
DataRow[] dr = dt.Select("AId = '" + GridView3.Rows[i].Cells[1].Text + "'");
chk.Checked = dr.Length > 0;
if (!chk.Checked)
{
chkAll.Checked = false;
}
}
}
}
}
private void BindSecondaryGrid()
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
GridView4.DataSource = dt;
GridView4.DataBind();
}
Dharam RaiPosted Jul 30, 2018, 12:44 AM
Dharam RaiPosted Jul 30, 2018, 12:43 AM
Jignesh KumarPosted Jul 28, 2018, 9:55 AM
Jignesh KumarPosted Jul 21, 2018, 5:10 AM
Dharam RaiPosted Jul 21, 2018, 5:01 AM
Jignesh KumarPosted Jul 21, 2018, 4:40 AM