I have a datagridview which I'm using for data entry. I've done this before with all text columns, and it worked great. But now I want one of the columns to be a databound combobox so the user can select options. When I do this, the resulting gridview's datasource ends up with empty rows (but the right quantity). What am I missing?
Here is code:
DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
{
cboCategory.HeaderText = "Category";
cboCategory.DataSource = downtimeCategories;
cboCategory.DisplayMember = "Name";
cboCategory.ValueMember = "CategoryID";
cboCategory.DataPropertyName = "CategoryID";
gridDowntime.Columns.Add(cboCategory);
}
Then code to grab gridview's datasource:
DataTable dt = (gridDowntime.DataSource as DataTable);
Everytime I get a table with the correct number of rows, but all the rows are empty (although they are long rows, the dataset visualizer has to scroll to show the entire cell). How can I find the error and correct?
EDIT: Is there some specific additional information I should provide here?
Here is a simple example project that I just cooked up.
The key thing I think you are getting wrong is that the DataPropertyName property of the DataGridViewComboboxColumn needs to refer to the datasource of the DataGridView, not the column.
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable("Customers");
DataColumn dc;
dc = new DataColumn();
dc.DataType = typeof(int);
dc.ColumnName = "CustomerID";
dt.Columns.Add(dc);
dt.Columns.Add(new DataColumn("LastName"));
dt.Columns.Add(new DataColumn("FirstName"));
// Concatenation of first and last names
dt.Columns.Add(new DataColumn("FullName"));
dt.Columns.Add(new DataColumn("Address"));
dt.Columns.Add(new DataColumn("City"));
dt.Columns.Add(new DataColumn("State"));
dt.Columns.Add(new DataColumn("Zip"));
dt.Columns.Add(new DataColumn("Phone"));
dc = new DataColumn();
dc.DataType = typeof(DateTime);
dc.ColumnName = "LastPurchaseDate";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = typeof(int);
dc.ColumnName = "CustomerType";
dt.Columns.Add(dc);
// Populate the table
dt.Rows.Add(2, "Baggins", "Bilbo", "Baggins, Bilbo", "Bagshot Row #1", "Hobbiton", "SH", "00001", "555-2222", DateTime.Parse("24/9/2008"), 1);
dt.Rows.Add(1, "Baggins", "Frodo", "Baggins, Frodo", "Bagshot Row #2", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("14/9/2008"), 1);
dt.Rows.Add(6, "Bolger", "Fatty", "Bolger, Fatty", "ProudFeet Creek", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("14/9/2008"), 1);
dt.Rows.Add(4, "Elessar", "Aragorn", "Elessar, Aragorn", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0000", DateTime.Parse("14/9/2008"), 4);
dt.Rows.Add(5, "Evenstar", "Arwin", "Evenstar, Arwin", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0001", DateTime.Parse("23/9/2008"), 4);
dt.Rows.Add(3, "Greyhame", "Gandalf", "Grayhame, Gandalf", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, 3);
DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
List<CustomerType> customerTypes = new List<CustomerType> { new CustomerType { Id = 1, Name = "Good" }, new CustomerType { Id = 4, Name = "Bad" }, new CustomerType { Id = 3, Name = "Ugly" } };
cboCategory.HeaderText = "Customer Type";
cboCategory.DataSource = customerTypes;
cboCategory.DisplayMember = "Name";
cboCategory.ValueMember = "Id";
cboCategory.DataPropertyName = "CustomerType";
dataGridView1.Columns.Add(cboCategory);
dataGridView1.DataSource = dt;
}
Bit of fluff in there - grabbed that datatable code right off the interwebs. But the key part here is the setting of properties for the combobox column.
My datasource is a list of customertype objects:
public class CustomerType
{
public int Id { get; set; }
public string Name { get; set; }
}
So I need to set DisplayMember on the column to "Name" and ValueMember to "Id" since this references the columns datasource. However for the DataPropertyName was set the value to "CustomerType" which is the name of a column in the DataTable that we have bound to the DataGridView.
So after a wee bit of discussion in chat it turns out that the above is true but that you also need to ensure that the types of your id columns match.
The datatable used to provide data to the combobox column was generated from a sql query and had the id column of type in. Your backing datatable had the id column made like so:
dt.Columns.Add(new DataColumn("Category"));
This defaults to a column of type string. Try instead something like:
downtimeEntries.Columns.Add("Category", typeof(int));
Related
How do create a DataTable in C#?
I did like this:
DataTable dt = new DataTable();
dt.clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
How do I see the structure of DataTable?
Now I want to add ravi for Name and 500 for Marks. How can I do this?
Here's the code:
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
DataRow _ravi = dt.NewRow();
_ravi["Name"] = "ravi";
_ravi["Marks"] = "500";
dt.Rows.Add(_ravi);
To see the structure, or rather I'd rephrase it as schema, you can export it to an XML file by doing the following.
To export only the schema/structure, do:
dt.WriteXMLSchema("dtSchemaOrStructure.xml");
Additionally, you can also export your data:
dt.WriteXML("dtDataxml");
You can also pass in an object array as well, like so:
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
object[] o = { "Ravi", 500 };
dt.Rows.Add(o);
Or even:
dt.Rows.Add(new object[] { "Ravi", 500 });
Create DataTable:
DataTable MyTable = new DataTable(); // 1
DataTable MyTableByName = new DataTable("MyTableName"); // 2
Add column to table:
MyTable.Columns.Add("Id", typeof(int));
MyTable.Columns.Add("Name", typeof(string));
Add row to DataTable method 1:
DataRow row = MyTable.NewRow();
row["Id"] = 1;
row["Name"] = "John";
MyTable.Rows.Add(row);
Add row to DataTable method 2:
MyTable.Rows.Add(2, "Ivan");
Add row to DataTable method 3 (Add row from another table by same structure):
MyTable.ImportRow(MyTableByName.Rows[0]);
Add row to DataTable method 4 (Add row from another table):
MyTable.Rows.Add(MyTable2.Rows[0]["Id"], MyTable2.Rows[0]["Name"]);
Add row to DataTable method 5 (Insert row at an index):
MyTable.Rows.InsertAt(row, 8);
// Create a DataTable and add two Columns to it
DataTable dt=new DataTable();
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Age",typeof(int));
// Create a DataRow, add Name and Age data, and add to the DataTable
DataRow dr=dt.NewRow();
dr["Name"]="Mohammad"; // or dr[0]="Mohammad";
dr["Age"]=24; // or dr[1]=24;
dt.Rows.Add(dr);
// Create another DataRow, add Name and Age data, and add to the DataTable
dr=dt.NewRow();
dr["Name"]="Shahnawaz"; // or dr[0]="Shahnawaz";
dr["Age"]=24; // or dr[1]=24;
dt.Rows.Add(dr);
// DataBind to your UI control, if necessary (a GridView, in this example)
GridView1.DataSource=dt;
GridView1.DataBind();
To add a row:
DataRow row = dt.NewRow();
row["Name"] = "Ravi";
row["Marks"] = 500;
dt.Rows.Add(row);
To see the structure:
Table.Columns
You can add Row in a single line
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
You can write one liner using DataRow.Add(params object[] values) instead of four lines.
dt.Rows.Add("Ravi", "500");
As you create new DataTable object, there seems no need to Clear DataTable in very next statement. You can also use DataTable.Columns.AddRange to add columns with on statement. Complete code would be.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name"), new DataColumn("Marks") });
dt.Rows.Add("Ravi", "500");
The easiest way is to create a DtaTable as of now
DataTable table = new DataTable
{
Columns = {
"Name", // typeof(string) is implied
{"Marks", typeof(int)}
},
TableName = "MarksTable" //optional
};
table.Rows.Add("ravi", 500);
DataTable dt=new DataTable();
Datacolumn Name = new DataColumn("Name");
Name.DataType= typeoff(string);
Name.AllowDBNull=false; //set as null or not the default is true i.e null
Name.MaxLength=20; //sets the length the default is -1 which is max(no limit)
dt.Columns.Add(Name);
Datacolumn Age = new DataColumn("Age", typeoff(int));`
dt.Columns.Add(Age);
DataRow dr=dt.NewRow();
dr["Name"]="Mohammad Adem"; // or dr[0]="Mohammad Adem";
dr["Age"]=33; // or dr[1]=33;
dt.add.rows(dr);
dr=dt.NewRow();
dr["Name"]="Zahara"; // or dr[0]="Zahara";
dr["Age"]=22; // or dr[1]=22;
dt.rows.add(dr);
Gv.DataSource=dt;
Gv.DataBind();
DataTable dt=new DataTable();
DataColumn Name = new DataColumn("Name",typeof(string));
dt.Columns.Add(Name);
DataColumn Age = new DataColumn("Age", typeof(int));`
dt.Columns.Add(Age);
DataRow dr=dt.NewRow();
dr["Name"]="Kavitha Reddy";
dr["Age"]=24;
dt.add.Rows(dr);
dr=dt.NewRow();
dr["Name"]="Kiran Reddy";
dr["Age"]=23;
dt.Rows.add(dr);
Gv.DataSource=dt;
Gv.DataBind();
You have to add datarows to your datatable for this.
// Creates a new DataRow with the same schema as the table.
DataRow dr = dt.NewRow();
// Fill the values
dr["Name"] = "Name";
dr["Marks"] = "Marks";
// Add the row to the rows collection
dt.Rows.Add ( dr );
In addition to the other answers.
If you control the structure of the DataTable there is a shortcut for adding rows:
// Assume you have a data table defined as in your example named dt
dt.Rows.Add("Name", "Marks");
The DataRowCollection.Add() method has an overload that takes a param array of objects. This method lets you pass as many values as needed, but they must be in the same order as the columns are defined in the table.
So while this is a convenient way to add row data, it can be risky to use. If the table structure changes your code will fail.
Question 1: How do create a DataTable in C#?
Answer 1:
DataTable dt = new DataTable(); // DataTable created
// Add columns in your DataTable
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
Note: There is no need to Clear() the DataTable after creating it.
Question 2: How to add row(s)?
Answer 2: Add one row:
dt.Rows.Add("Ravi","500");
Add multiple rows: use ForEach loop
DataTable dt2 = (DataTable)Session["CartData"]; // This DataTable contains multiple records
foreach (DataRow dr in dt2.Rows)
{
dt.Rows.Add(dr["Name"], dr["Marks"]);
}
Create datatabe with 2 columns: Name & Marks
IList columns = new List() {"Name", "Marks"};
Datatabel dt = new Datatable();
foreach (string column in columns)
dtSalesOrder.Columns.Add(column, typeof(string));
Add data to datatable
dt.Rows.Add("Ravi","500");
Is there any way to do an increment number from 1,2,3 and so on for my column in my datatable?
For example similar to column "Code" below
or at least is there any other way that i can name the numbers manually?
Here's what I've tried (but not working)
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;
Thanks for your reply
AutoIncrement is indeed the right approach here. You might be missing something else, but the following code does work:
var dt = new DataTable();
dt.Columns.Add(new DataColumn { ColumnName = "Code", AutoIncrement = true, DataType = typeof(int) });
dt.Columns.Add(new DataColumn { ColumnName = "Resource Type", DataType = typeof(string) });
dt.Columns.Add(new DataColumn { ColumnName = "Number of hits", DataType = typeof(int) });
var newRow = dt.NewRow();
newRow[1] = "Testing";
newRow[2] = 3;
dt.Rows.Add(newRow);
var newRow2 = dt.NewRow();
newRow2[1] = "Testing 2";
newRow2[2] = 6;
dt.Rows.Add(newRow2);
And when inspecting dt, the result is:
Code Resource Type Number of hits
0 Testing 3
1 Testing 2 6
I have created one datatable:
DataTable dt=new DataTable();
dt.Columns.Add("Country", typeof(string))
dt.Columns.Add("place", typeof(string))
dt.Columns.Add("Price", typeof(string))
dt.Columns.Add("Desc", typeof(string))
and I have inserted some data in this datatable:
DataRow dtrow = dt.NewRow(); // Create New Row
dtrow["Country"] = "India"; //Bind Data to Columns
dtrow["place"] = "wizag";
dtrow["Price"] = "7520";
dtrow["Desc"] = "Anywhere";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row
dtrow["Country"] = "India"; //Bind Data to Columns
dtrow["place"] = "Goa";
dtrow["price"] = "4500";
dtrow["Desc"] = "Anything";
dt.Rows.Add(dtrow);
I have bind this datatable with my grid view. I have also added one textbox and one search button in mmy aspx page. My requirement is that I want to search data based on user input e.g like 4500 or wizag, and if country name is the same I want to show it in one row
How can I do this?
I would use a DataView in this case
DataView dv = new DataView(dt);
dv.RowFilter = "price = 4500 AND Country = 'India'";
GridView.DataSource = dv.ToTable();
GridView.DataBind();
I trying to change the column name of the DataTable but some how I lost the data.
foreach (DataRow dr in item)
Ds.Tables[0].ImportRow(dr);
Table.Columns[0].ColumnName = "Barcode";
Table.Columns[1].ColumnName = "Description";
Table.Columns[2].ColumnName = "Price";
Table.Columns[3].ColumnName = "Cost";
Table.Columns[4].ColumnName = "Stock";
Table.Columns[5].ColumnName = "Dept #";
updateDgv.DataSource = Table;
First, the correct code is:
DataTable table = new DataTable();
table.Columns.Add("ItemNo", typeof(string));
table.Columns[0].ColumnName = "Item #";
After your code runs, you have a DataTable with one column named Item #.
There are no rows or data because you didn't add any data. So you didn't lose anything.
I have tested your code and it seems to work fine:
DataTable table = new DataTable();
table.Columns.Add("ItemNo", typeof(string));
table.Columns.Add("ItemName", typeof(string));
DataRow dr = table.NewRow();
dr[0] = "1";
dr[1] = "Name1";
table.Rows.Add(dr);
dr = table.NewRow();
dr[0] = "1";
dr[1] = "Name1";
table.Rows.Add(dr);
table.Columns[0].ColumnName = "Item #";
Probably you are trying to access data through column name later in the code and that is where it is failing. You will not loose the data by just renaming the column name. (Also its a not a good practice use special characters in table column names)
Check out the following screen shots from data visualizer
Before Column Rename:
After Column Rename:
I build up my datagrid with binding source:
SqlDataAdapter adapter = new SqlDataAdapter(Datenbank.cmd);
dataSet1.Tables.Clear();
adapter.Fill(dataSet1, "Table");
bs = new BindingSource();
bs.DataSource = dataSet1.Tables["Table"];
dataGridView1.DataSource = bs;
Now i sort it:
bs.Sort = "customer DESC";
Now I want to add new row at 0
dataSet1.Tables[0].Rows.InsertAt(newRow, 0);
However, it will not be insert at position 0
Similar problem with deleting at position x - which was solved in here
>> stack overflow question
thought of using bs.insert(0,newRow) but it says external objects can not be added to this list
How to add a row in bindingsource on given position after sorting?
Here is a solution that works for adding to the top of the list - I have no idea if it is the best solution but it works. There are lots of other ways to approach the issue, including things like making your own binding collection that allows inserting into a sorted list, that could give better code form certain points of view, but this is simple at least.
Here is the code, which includes creating a datatable.
public partial class Form1 : Form
{
BindingSource bs;
DataTable dt;
public Form1()
{
InitializeComponent();
dt = new DataTable("Customers");
DataColumn dc;
dc = new DataColumn();
dc.DataType = typeof(int);
dc.ColumnName = "CustomerID";
dt.Columns.Add(dc);
dt.Columns.Add(new DataColumn("LastName"));
dt.Columns.Add(new DataColumn("FirstName"));
// Concatenation of first and last names
dt.Columns.Add(new DataColumn("FullName"));
dt.Columns.Add(new DataColumn("Address"));
dt.Columns.Add(new DataColumn("City"));
dt.Columns.Add(new DataColumn("State"));
dt.Columns.Add(new DataColumn("Zip"));
dt.Columns.Add(new DataColumn("Phone"));
dc = new DataColumn();
dc.DataType = typeof(DateTime);
dc.ColumnName = "LastPurchaseDate";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = typeof(int);
dc.ColumnName = "CustomerType";
dt.Columns.Add(dc);
dt.Columns.Add("HiddenSort", typeof(bool));
// Populate the table
dt.Rows.Add(2, "Baggins", "Bilbo", "Baggins, Bilbo", "Bagshot Row #1", "Hobbiton", "SH", "00001", "555-2222", DateTime.Parse("9/9/2008"), 1, false);
dt.Rows.Add(1, "Baggins", "Frodo", "Baggins, Frodo", "Bagshot Row #2", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("9/9/2008"), 1, false);
dt.Rows.Add(6, "Bolger", "Fatty", "Bolger, Fatty", "ProudFeet Creek", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("9/9/2008"), 1); dt.Rows.Add(4, "Elessar", "Aragorn", "Elessar, Aragorn", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0000", DateTime.Parse("9/9/2008"), 4, false);
dt.Rows.Add(5, "Evenstar", "Arwin", "Evenstar, Arwin", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0001", DateTime.Parse("9/9/2008"), 4, false);
dt.Rows.Add(3, "Greyhame", "Gandalf", "Grayhame, Gandalf", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, 3, false);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
dataGridView1.Columns["HiddenSort"].Visible = false;
dataGridView1.Sorted += new EventHandler(dataGridView1_Sorted);
}
void dataGridView1_Sorted(object sender, EventArgs e)
{
foreach (DataRow dr in dt.Rows)
{
dr["HiddenSort"] = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
DataRow dr = dt.Rows.Add(2, "Testing", "Bilbo", "Baggins, Bilbo", "Bagshot Row #1", "Hobbiton", "SH", "00001", "555-2222", DateTime.Parse("9/9/2008"), 1, true);
DataView dv = dt.DefaultView;
if (dataGridView1.SortedColumn == null)
{
dv.Sort = "[HiddenSort] desc";
}
else
{
string sortname = dataGridView1.SortedColumn.Name;
dv.Sort = "[HiddenSort] desc, [" + sortname + "] asc";
}
}
}
The trick here is adding a new column HiddenSort to the datatable that allows us to sort the datatable based on whether a row is new or not. That coupled with grabbing the sort column from the DataGridView allows up to keep the old sort order and maintain the position of new rows.