C#: Bind XML to ComboBox via DataSet - c#

I am trying to get this code work for about 2 hours =( I am new to C# and don't know all the .NET library classes.
The target is to populate XML data to comboBox
DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("table1");
dataTable.Columns.Add("col1", typeof(string));
dataSet.Tables.Add(dataTable);
StringReader strR = new StringReader("<root><parm1>val1</parm1><parm2>val2</parm2></root>");
dataSet.ReadXml(strR);
comboBox1.DataSource = dataSet.Tables[0];
comboBox1.DisplayMember = "col1";
comboBox1.ValueMember = "col1";
Well, it doesn't work as expected. The ComboBox should show
val1
val2
I don't really understand how column names of DataTable in a DataSet are related to XML-Tags... Maybe that's the point?
Thank You in advance!

The following should work:
DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("table1");
dataTable.Columns.Add("col1", typeof(string));
dataSet.Tables.Add(dataTable);
StringReader strR = new StringReader("<root><table1><col1>val1</col1></table1><table1><col1>val2</col1></table1></root>");
dataSet.ReadXml(strR);
comboBox1.DataSource = dataSet.Tables[0];
comboBox1.DisplayMember = "col1";
comboBox1.ValueMember = "col1";
The names of the tables and the columns need to be consistent between your C# objects and XML data.

Related

How to fill DataGridViewComboBoxColumn from a database table column without using datareader

Here is my code:
SqlDataAdapter GridDataAdapter = new SqlDataAdapter(query, con);
DataSet GridDataSet = new DataSet();
GridDataAdapter.Fill(GridDataSet, tbln);
dataGridView1.DataSource = GridDataSet;
DataGridViewComboBoxColumn dgvCB = new DataGridViewComboBoxColumn();
dataGridView1.DataMember = tbln;
Here, I want to fetch data for DataGridViewComboBox from a database table column.
How can I fill the DataGridViewComboBoxColumn without using a DataReader?
You already have the dataset filled, you could just iterate over the row collection if it contains the values that you want the DGVCombo to contain. So, the most straightforward way would be something along the lines of:
foreach(DataRow r in GridDataSet.Tables[0].Rows)
{
dgvCB.Items.Add(r["MyColumn"]);
}
Where .Tables[0] has the column("MyColumn") that you are looking for...
This should be what you need...
DataGridViewComboBoxColumn dgvCB = new DataGridViewComboBoxColumn();
dgvCB.Name = "lastname";
dgvCB.DataSource = tbln;
dgvCB.HeaderText = "Last";
//uncomment this to actually select the value in the combo box
//dgvCB.DataPropertyName = "lastname";
dgvCB.ValueMember = "lastname";
dataGridView1.Columns.Add(dgvCB);

Bind ComboBox to DataSet in code behind (no XAML)

How to bind a Combo Box to DataSet in code behind, without using XAML at all:
I tried the following, but all my combobox items are "System.Data.DataRowView" instead of the actual value. What is wrong?
string str = #"SELECT * FROM FooTable";
da.SelectCommand = new SqlCeCommand(str, connection);
da.Fill(devDs, "FooTable");
dt = ds.Tables["FooTable"];
comboBox1.ItemsSource = devDt.DefaultView;
You will have to set DisplayMemberPath property
combobox.DisplayMemberPath = "ColumnName"
You can use comboBox1.DisplayMemberPath to set which column in your table should be used for UI presentation.
Test sample:
var dataTable = new DataTable();
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Rows.Add(1, "Test1");
dataTable.Rows.Add(2, "Test2");
comboBox1.ItemsSource = dataTable.DefaultView;
comboBox1.DisplayMemberPath = "Name";

add data to DataGrid View through DataSet?

I have a datagridview that is bound to a dataset
I defined the table and the columns, how I can programmaticlly insert data to it?
I did this
http://msdn.microsoft.com/en-us/library/5ycd1034%28VS.71%29.aspx
but it did not show the data in my DataGridView !!!
now you need to assign the values to the datagridview
DataTable dt = new DataTable();
// Add rows to dt
datagridview.DataSource = dt;
Sample example
SqlDataAdapter da = new SqlDataAdapter(quryString, con);
DataSet ds = new DataSet();
da.Fill(ds, "Emp");
//dataGridView1.DataSource = ds.Tables["Emp"];
dataGridView1.Columns.Add("EmpID", "ID");
dataGridView1.Columns.Add("FirstName", "FirstName");
dataGridView1.Columns.Add("LastName", "LastName");
int row = ds.Tables["Emp"].Rows.Count - 1;
for (int r = 0; r<= row; r++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[r].Cells[0].Value = ds.Tables["Emp"].Rows[r].ItemArray[0];
dataGridView1.Rows[r].Cells[1].Value = ds.Tables["Emp"].Rows[r].ItemArray[1];
dataGridView1.Rows[r].Cells[2].Value = ds.Tables["Emp"].Rows[r].ItemArray[2];
}
it can be bounded directly by .
DataSet ds = new DataSet();
in C#
DataGridView.DataSource = ds.Tables["your Table index which you want to bind with datagridview"];
Like
DataGridView.DataSource = ds.Tables[0];
In VB
DataGridView.DataSource = ds.Tables("your Table index which you want to bind with datagridview")
like
DataGridView.DataSource = ds.Tables(0)

datacoloumn in asp.net

I first want to add a database column in DataColumn..and then i have to add datacoloumn in datatable.. please guide me;
SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from exam_cyctstmarkdet",con);
DataColumn c = new DataColumn();
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");
Is the bit you're missing
DataTable DT = new DataTable;
d2.Fill(DT);
DT.Columns.Add(c);
???
If you wish to create a DataTable, and add your own columns to it, you can try
DataTable dt = new DataTable("MyTable");
DataColumn col = dt.Columns.Add("parta", typeof(int));
You only have to keep a reference to the column if you plan to use it somewhere, else you can try
DataTable dt = new DataTable("MyTable");
dt.Columns.Add("parta", typeof(int));
I would suggest that you use the SqlDataAdapter to populate a DataSet, which will contain a DataTable. I assume you want to add the new column to this DataTable.
Here's how to do that:
SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from exam_cyctstmarkdet",con);
DataSet dst = new DataSet();
d2.Fill(dst); // now you have a populated DataSet containing a DataTable
DataTable dt = dst.Tables[0]; // I created this variable for clarity; you don't really need it
DataColumn c = new DataColumn();
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");
dt.Add(c);
After you add the Column to the DataTable, the Column will be empty.
You can populate it in code.

How to use combo box in c#

I have no idea where to start. i tried DataTable but it didn't work.(This is an easy question :) )
I tried everything
{
var test = new DataTable();
test.Columns.Add("test");
test.TableName = "test";
test.Columns.Add("test");
comboBox1.DataSource = test.XXXX ;
}
Assuming you mean winforms, something like:
DataTable test = new DataTable();
test.TableName = "test";
test.Columns.Add("foo", typeof(string));
test.Columns.Add("bar", typeof(int));
test.Rows.Add("abc", 123);
test.Rows.Add("def", 456);
ComboBox cbo = new ComboBox();
cbo.DataSource = test;
cbo.DisplayMember = "foo";
cbo.ValueMember = "bar";
Form form = new Form();
form.Controls.Add(cbo);
Application.Run(form);
(in particular, SelectedValue should give you the 123 and 456 - useful for ids, etc)
ComboBox.Items property, unless you want data from a database or something.
DataTable dt=new DataTable();
dt.Columns.Add("Col1",typeof(int));
dt.Columns.Add("Col2",typeof(String));
dt.Rows.Add(1,"A");
dt.Rows.Add(2,"B");
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Col2";
comboBox1.ValueMember = "Col1";
You'll need to set the 'DataItemField' and 'DataValueField' to the appropriate column names in your datatable.

Categories

Resources