Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid from a DataView

Status
Not open for further replies.

Pipe2Path

Programmer
Aug 28, 2001
60
0
0
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 ;
}

 
Where is your data table defined? What is in the Page_Load event?
 
Thanks jbenson, I got it figured out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top