Pipe2Path
Programmer
- Aug 28, 2001
- 60
I have a datagrid that is not populated from a database, but from a textbox everytime the ADD button is clicked.
I create a datatable with 2 columns, and assign it to a dataview and rebind the datagrid. All works well, until I have a second or third record to add to the datagrid. It keeps rebinding again, and I am only able to add a single record into the datagrid.
Any suggestions on how I could add to the datagrid, rather than replace the one record?
It seems, everytime I click the ADD button,the page is reloaded as it's on the server side. How can I persist the datatable, so it keeps building up instead of getting rebuilt everytime?
Thanks in advance.
Here's the sample code :
private void btnAdd_Click(object sender, System.EventArgs e)
{
string PartID = null ;
string PartDescription = null ;
ValidatePart() ;
PartID = this.txtPartID.Text ;
PartDescription = this.txtPartDescription.Text ;
if (this.txtPartDescription.Text != "")
{
AddToDataTable (PartID, PartDescription, ref dt );
DataView dv = new DataView (dt) ;
this.grdParts.DataSource = dv ;
if (this.IsPostBack == true)
this.grdParts.DataBind () ;
}
private DataTable AddToDataTable(string PartID, string PartDescription, ref DataTable dt)
{
DataRow PartRow;
PartRow = dt.NewRow() ;
PartRow["Part ID"] = PartID;
PartRow["Part Description"] = PartDescription ;
dt.Rows.Add(PartRow) ;
return dt ;
}
I create a datatable with 2 columns, and assign it to a dataview and rebind the datagrid. All works well, until I have a second or third record to add to the datagrid. It keeps rebinding again, and I am only able to add a single record into the datagrid.
Any suggestions on how I could add to the datagrid, rather than replace the one record?
It seems, everytime I click the ADD button,the page is reloaded as it's on the server side. How can I persist the datatable, so it keeps building up instead of getting rebuilt everytime?
Thanks in advance.
Here's the sample code :
private void btnAdd_Click(object sender, System.EventArgs e)
{
string PartID = null ;
string PartDescription = null ;
ValidatePart() ;
PartID = this.txtPartID.Text ;
PartDescription = this.txtPartDescription.Text ;
if (this.txtPartDescription.Text != "")
{
AddToDataTable (PartID, PartDescription, ref dt );
DataView dv = new DataView (dt) ;
this.grdParts.DataSource = dv ;
if (this.IsPostBack == true)
this.grdParts.DataBind () ;
}
private DataTable AddToDataTable(string PartID, string PartDescription, ref DataTable dt)
{
DataRow PartRow;
PartRow = dt.NewRow() ;
PartRow["Part ID"] = PartID;
PartRow["Part Description"] = PartDescription ;
dt.Rows.Add(PartRow) ;
return dt ;
}