DataRow problem or bug in C#?

E

Eddie B.

I am trying to create a new row with the values from textboxes. When I
create the first row it works fine but when I click the button to create
another row it only wverwrites the existing row. this is what I am doing.
Toward the bottom (about 8 uncommented lines from
bottom) where DataRow first appears is where my trouble starts.
//Create DataSet

DataSet ds = new DataSet();

//Create DataTable

DataTable dt = new DataTable("Meds");

//Add DataTable to DataSet

ds.Tables.Add(dt);

//Add Columns to DataTable

DataColumn dc = new DataColumn("Drug");

ds.Tables["Meds"].Columns.Add(dc);

DataColumn dc2 = new DataColumn("Dose");

ds.Tables["Meds"].Columns.Add(dc2);

DataColumn dc3 = new DataColumn("Form");

ds.Tables["Meds"].Columns.Add(dc3);

DataColumn dc4 = new DataColumn("Route");

ds.Tables["Meds"].Columns.Add(dc4);

DataColumn dc5 = new DataColumn("Frequency");

ds.Tables["Meds"].Columns.Add(dc5);

DataRow tr = dt.NewRow();

tr["Drug"] = this.textBox11.Text;

tr["Dose"] = this.textBox12.Text;

tr["Form"] = this.textBox13.Text;

tr["Route"] = this.textBox14.Text;

tr["Frequency"] = this.textBox15.Text;


//Add rows to data table

dt.Rows.Add(tr);


//Display in dataGrid

this.dataGrid3.DataSource = dt;
 
N

Norman Yuan

If you put all the code you listed inside the button_click event handler, of
course you are overwriting the datarow with each button clicking, actually,
you are overwriting entire dataset.

You should put code for creating DataSet (and DataTable in the DataSet, and
DataColumns in the DataTable) and binding it to DataGrid in Form class, and
only put "DataRow tr = dt.NewRow();..." in button_click event handler.
 
A

Anibal Acosta

I'am not very familiarized with data tables and rows but I think that...
tr["Drug"] = this.textBox11.Text;

tr["Dose"] = this.textBox12.Text;

tr["Form"] = this.textBox13.Text;

tr["Route"] = this.textBox14.Text;

tr["Frequency"] = this.textBox15.Text;

You are adding columns to your row.
You must create one instance of DataRow for each row that you want to add.




Eddie B. said:
I am trying to create a new row with the values from textboxes. When I
create the first row it works fine but when I click the button to create
another row it only wverwrites the existing row. this is what I am doing.
Toward the bottom (about 8 uncommented lines from
bottom) where DataRow first appears is where my trouble starts.
//Create DataSet

DataSet ds = new DataSet();

//Create DataTable

DataTable dt = new DataTable("Meds");

//Add DataTable to DataSet

ds.Tables.Add(dt);

//Add Columns to DataTable

DataColumn dc = new DataColumn("Drug");

ds.Tables["Meds"].Columns.Add(dc);

DataColumn dc2 = new DataColumn("Dose");

ds.Tables["Meds"].Columns.Add(dc2);

DataColumn dc3 = new DataColumn("Form");

ds.Tables["Meds"].Columns.Add(dc3);

DataColumn dc4 = new DataColumn("Route");

ds.Tables["Meds"].Columns.Add(dc4);

DataColumn dc5 = new DataColumn("Frequency");

ds.Tables["Meds"].Columns.Add(dc5);

DataRow tr = dt.NewRow();

tr["Drug"] = this.textBox11.Text;

tr["Dose"] = this.textBox12.Text;

tr["Form"] = this.textBox13.Text;

tr["Route"] = this.textBox14.Text;

tr["Frequency"] = this.textBox15.Text;


//Add rows to data table

dt.Rows.Add(tr);


//Display in dataGrid

this.dataGrid3.DataSource = dt;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top