Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Good Evening,

This is the method when clicking a button

protected void btnAdd_Click(object sender, EventArgs e)
{
if (cboSources.SelectedIndex != -1)
{
grdViewDisease.DataSource = GetDataTable(cboSources.SelectedValue);
grdViewDisease.DataBind();
}

}

This is for binding data and calling in the above method

private DataTable GetDataTable(string itemID)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Disease", typeof(string));
dt.Columns.Add("Source", typeof(string));
dt.Rows.Add(itemID, hidSource.Attributes["value"], cboSources.SelectedItem);
return dt;
}

So after adding a new row and binding it to the grid I can add a new row but old row values are not shown

So please explain how to retain all the values?

Thank You
Posted

Well you are adding only one row.In your GetDataTable method,you are creating the new datatable everytime and returing that new datatable.Because of this it will return new values and vanish old value.

For solving this issue,you just globally declare Datatable variable and check.
 
Share this answer
 
Try this!!
C#
DataTable dt = null;



protected void btnAdd_Click(object sender, EventArgs e)
 {
 if (cboSources.SelectedIndex != -1)
 {
 grdViewDisease.DataSource = GetDataTable(cboSources.SelectedValue);
 grdViewDisease.DataBind();
 }

 }
 



private DataTable GetDataTable(string itemID)
 {
if(viewstate["dt"]==null)
{
  dt = new DataTable();
 
 dt.Columns.Add("ID", typeof(string));
 dt.Columns.Add("Disease", typeof(string));
 dt.Columns.Add("Source", typeof(string));
}
else
{
  dt=(DataTable)viewstate["dt"];
}
 
 dt.Rows.Add(itemID, hidSource.Attributes["value"], cboSources.SelectedItem);
 viewstate["dt"]=dt;
 return dt;
 }
 
Share this answer
 
Comments
Member 8557048 28-Jan-14 8:57am    
Thank You very much its working fine
Dinesh.V.Kumar 29-Jan-14 0:02am    
Thanks..am happy that its working fine!!!coz i did not check it here...Kindly mark it as answer....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900