Filling of ComboBox with Dataset - c#

When am trying to fill Combobox
con.Open();
da = new SqlDataAdapter("Mt_Post_select",con);
//Mt_Post is Stored procedure with Select Command
ds1 = new DataSet();
da.Fill(ds1, "Mt_Post");
// The table has only one row
comboBox1.DataSource = ds1.Tables[0];
comboBox1.DisplayMember = "Mt_Post";
It bind But shows instead of data it having System.Data.DataRowView
What is the wrong this code Please anyone tell me

Replace Mt_Post in comboBox1.DisplayMember = "Mt_Post"; with the name of column whose value you would like to display.

I hope the following sample helps;
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Email");
dt.AcceptChanges();
DataRow dr0 = dt.NewRow();
dr0[0] = "Kadir Sumerkent";
dr0[1] = "kadir#sumerkent.com";
DataRow dr1 = dt.NewRow();
dr1[0] = "Kadir Sumerkent 2";
dr1[1] = "kadir#sumerkent2.com";
dt.Rows.Add(dr0);
dt.Rows.Add(dr1);
dt.AcceptChanges();
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Email";
comboBox1.DataSource = dt;

Let's try this:
Suppose your Stored Procedure is like the code below:
SELECT
Users.User_name AS username
Users.User_Code AS userID
FROM Users
-- WHERE CONDITONS ARE APPLIED
Now in order to Fetch this DataSet you are going to do as follows:
// YOUR OWN CODE
con.Open();
da = new SqlDataAdapter("Mt_Post_select",con);
//Mt_Post is Stored procedure with Select Command
ds1 = new DataSet();
// JUST FILL THE DataSet in THIS WAY
da.Fill(ds1);
// JUST TO MAKE SURE WE HAVE AN EMPTY COMBOBOX
comboBox1.Items.Clear();
comboBox1.DataSource = null;
// NOW POPULATE comboBox1 LIKE THIS
if (ds.Tables[0].Rows.Count > 0)
{
comboBox1.DataSource = ds1.Tables[0];
comboBox1.DisplayMember = "username";
comboBox1.ValueMember = "userID"
}

Related

DataGridView add column by code

I need to add columns to my DataGridView dynamically... to do this here is my code:
dgv.Columns.Clear();
dgv.AutoGenerateColumns = false;
for (int i = 0; i < fields.Length; i++)
{
var newColumn = new DataGridViewColumn(new DataGridViewTextBoxCell());
newColumn.Name = fields[i];
newColumn.DataPropertyName = fields[i];
newColumn.HeaderText = fields[i];
dgv.Columns.Add(newColumn);
}
And after, when I link my query to the DataSource I retrieve this error:
The value can't be null.
Parameter name: columnName
And the strange thing is that here is my DataGridView/DataTable situation:
The names seems right...
The other strange thing is that the first row is shown, but the others gives the exception (the query is OK and the data are valid...)
here is the code to create the DataSource for the table:
public DataTable getData()
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(item.Query, db.getConnection());
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
dap.Fill(ds);
return ds.Tables[0];
}
and after:
dgv.DataSource = getData();

How to set combobox value to null or empty while loading data to it from database in c#.net

I have a windows form coded in c#..
the problem is I have a combobox which is loaded values from database,and that code has written in LOAD form..
so,my form's combobox values are generated automatically to it and the Initial combobx text is loaded to it..
I want to set combobox initial text or value should be empty or null..
please help me to solve it..
Thanx in advance
my code is as follows
try
{
ConnectionStringSettings consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"];
string connectionString = consettings.ConnectionString;
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
SqlCommand cmd = new SqlCommand("select employee_id,employee_name,image from Employee_Details", cn);
SqlDataReader dtr;
dtr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("employee_id", typeof(string));
dt.Columns.Add("employee_name", typeof(string));
dt.Load(dtr);
//Convert.ToString.comboBox1.Items.Insert("", 0);
comboBox1.DisplayMember = "employee_id";
comboBox1.DisplayMember = "employee_name";
comboBox1.DataSource = dt;
//dateTimePicker1.Enabled = true; ;
cn.Close();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
Add this line after you set your datasource to db:
comboBox1.SelectedItem = null;
Or
comboBox1.SelectedItem = -1;
do not make it null, better to writer it Select as selected value.
`dt.Load(dtr);
DataRow drow = dt.NewRow();
drow[0] = "-1";
drow[1] = "Select";
drow[2] = string.Empty;
dt.Rows.InsertAt(drow, 0);
dt.AcceptChanges();
comboBox1.DisplayMember = "employee_id";
comboBox1.DisplayMember = "employee_name";
comboBox1.DataSource = dt;`

How to show dataset changes in datagridview?

I have a code like below.
I a dataset and two tables in it. And there is a relation between those tables.
DataSet ds = new DataSet();
DataTable dtPerson = new DataTable("Persons");
DataTable dtBooks = new DataTable("Books");
ds.Tables.Add(dtPerson);
ds.Tables.Add(dtBooks);
dtPersons = LoadPersons();
dtBooks = LoadBooks();
ds.Relations.Add(new DataRelation("PersonBookRel",dtPersons.Columns["P_ID"],dtBooks.Columns["P_ID"]));
gridPersons.DataSource = ds;
gridPersons.DataMember = "Persons";
gridBooks.DataSource = ds;
gridBooks.DataMember = "Books";
The problem is that, when i add new row to dtPerson, gridPerson does not show new records.
Code is below.
DataRow row = dtPerson.NewRow();
row[0] = "Joe";
row[1] = "Black"
row[2] = 18;
...
dtPerson.Rows.Add(row);
ds.AcceptChanges();
So what is the problem? How to solve it?

System.Data.DataRowView, c#

I've got a problem:
SqlDataAdapter da = new SqlDataAdapter("SELECT dbo.User.name FROM dbo.User", con);
DataTable dt = new DataTable();
comboBox1.DataSource = dt;
da.Fill(dt);
It returned System.Data.DataRowView in every row.
I tried:
comboBox1.ValueMember = "idUser";
comboBox1.DisplayMember = "dbo.User.name"
and I get error:
Cannot bind to the new display member. Parameter name:
newDisplayMember
I want to get idUser and name.
Thanks
You need to return the ID in your SELECT statement and assign the DataTable as the DataSource after you have filled the DataTable:
SqlDataAdapter da = new SqlDataAdapter("SELECT IDUser, Name FROM dbo.User", con);
DataTable dt = new DataTable();
da.Fill(dt);
You can then set up the ComboBox as such:
comboBox1.DataSource = dt.DefaultView;
comboBox1.ValueMember = "IDUser";
comboBox1.DisplayMember = "Name"
UPDATE
If you want to access the selected text or value of the item selected in the ComboBox, then you will need to do something like:
DataRowView drvItem = comboBox1.SelectedItem as DataRowView;
if (drvItem != null)
{
label4.Text = drvItem["ID"].ToString();
label3.Text = drvItem["Name"].ToString();
}
Please use below
SqlDataAdapter da = new SqlDataAdapter("SELECT IDUser, Name FROM dbo.User", con);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "IDUser";
comboBox1.DisplayMember = "Name";

How to rename the datatable column name without losing the data?

Q:
I want to rename my data table column names .
I tried this :
dt.Columns[8].ColumnName = "regnum";
dt.AcceptChanges();
but my data is lost after that !!
dt.Columns[8].ColumnName = "regnum";
This just binds your Columns[8] to the non-existing "regnum" column in the Db.
If you want to rename the actuals Db column, execute an SQL script.
But my guess is you actually want to change the Caption:
dt.Columns[8].Caption = "regnum";
Following is the example:
DataTable Dt = new DataTable();
DataColumn Dc = new DataColumn("Name");
DataColumn Dc1 = new DataColumn("ID");
Dt.Columns.Add(Dc);
Dt.Columns.Add(Dc1);
DataRow dr = Dt.NewRow();
dr["name"] = "1";
dr["ID"] = "111";
Dt.Rows.Add(dr);
dr = Dt.NewRow();
dr["name"] = "2";
dr["ID"] = "11112";
Dt.Rows.Add(dr);
Dt.Columns[0].ColumnName = "ddsxsd";
Dt.AcceptChanges();
I did not find any data loss!!!!!!!! Because it will merely change the column name.
EDIT
You can also bring your desired column names from your Stored Procedures.
Try this:
dataTable.Columns["ExistingColumnName"].ColumnName = "regnum";
Also may this code serve someone!
I have a realize that this is the easiest way just like #Henk said:
using (SqlDataAdapter da = new SqlDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
dt.Columns[0].ColumnName = "Item NO";
dt.Columns[1].ColumnName = "LocalCode";
dt.Columns[2].ColumnName = "Currency";
dt.Columns[3].ColumnName = "Menu Flag";
dt.Columns[4].ColumnName = "Item Class";
dt.Columns[4].ColumnName = "Dress Sort";
return dt;
}

Categories