[Mono-dev] Adding data to a dataset question
Morten Mertner
mono-devel-list at lists.ximian.com
Wed Aug 10 08:14:50 EDT 2005
Hi Paul,
you are modifying the existing datarow reference, and instead need to
create a new DataRow instance.
Yours,
Morten
Paul F. Johnson wrote:
>Hi,
>
>I'm trying to add multiple lines to a dataset but keep hitting the same
>problem. Now, I can get around the problem, but I'm not happy with how I have
>to do it.
>
>The code looks like this
>
> private void FillDataSet()
> {
> // create a new DataTable in the DataSet
> dsSource.Tables.Add("sites");
> dsSource.Tables["sites"].Columns.Add("url", Type.GetType("System.String"));
> dsSource.Tables["sites"].Columns.Add("name",
>Type.GetType("System.String"));
>
> // create a few rows and add them to the DataTable
> DataRow tempRow = dsSource.Tables["sites"].NewRow();
> tempRow["url"] = "http://www.gotmono.com";
> tempRow["name"] = "Mono community page";
> dsSource.Tables["sites"].Rows.Add(tempRow);
>
> tempRow["url"] = "http://www.go-mono.com/tutorial";
> tempRow["name"] = "The Mono handbook";
> dsSource.Tables["sites"].Rows.Add(tempRow);
>
> tempRow["url"] = "http://www.go-mono.com";
> tempRow["name"] = "The main Mono website";
> dsSource.Tables["sites"].Rows.Add(tempRow);
>
> tempRow["url"] =
>"http://www.nullenvoid.com/mono/wiki/index.php/WineSamples";
> tempRow["name"] = "Mono WineSamples";
> dsSource.Tables["sites"].Rows.Add(tempRow);
> }
>
> private void BindControls()
> {
> // Bind the listBox
> listBox1.DataSource = dsSource.Tables["sites"];
> listBox1.DisplayMember = "name";
> listBox1.ValueMember = "url";
>
> // Bind the combobox
> comboBox1.DataSource = dsSource.Tables["sites"];
> comboBox1.DisplayMember = "name";
> comboBox1.ValueMember = "url";
>
> // Bind the DataGrid to the DataSet
> dataGrid.DataSource = dsSource;
>
> // Bind the second DataGrid to the DataSet
> dataGrid2.DataSource = dsSource.Tables["sites"];
>
> // Bind the Textboxes to the selected row
> textBox.DataBindings.Add("text", dsSource.Tables["sites"], "name");
> textBox2.DataBindings.Add("text", dsSource.Tables["sites"], "url");
> }
>
>Clearly, the problem is coming from using temp constantly in the setting of
>the data (if I comment out all but the first dataadd set, the program works
>correctly-ish). The solution looks to be use an array of temps to add the
>data.
>
>If the dataset type was an int, I could say ds.AutoIncrement (according to
>MSDN).
>
>Is there a way to reuse temp or is the problem in that when the data is added,
>the row counter is not incremented and so I'm trying to overwrite the same
>data time and again?
>
>TTFN
>
>Paul
>
>
More information about the Mono-devel-list
mailing list