I have an application with a DataGridView. One of the columns is of the type Combobox. I want to add the items for this combobox programmatically. Here is the code I use for that:
this.dsStatussen = this.statussenMan.getAllStatussen();
DataGridViewComboBoxColumn cd = (DataGridViewComboBoxColumn)this.dgvEenheden.Columns[3];
cd.DataSource = dsStatussen;
cd.DisplayMember = "statussen";
cd.DataPropertyName = "sid";
cd.ValueMember = "status";
Then when I try to add a row I get the following error: "There is no field with the name status". I transelated the error to English because I have a Dutch error.
Here is the code I use for adding the rows:
Eenheden eenhedenMan = new Eenheden(objEvenement.eid);
DataSet EenhedenData = eenhedenMan.getAllEenheden();
foreach (DataRow dr in EenhedenData.Tables[0].Rows)
{
dgvEenheden.Rows.Add(
dr[0].ToString(),
dr[1].ToString(),
dr[2].ToString(),
Convert.ToInt32(dr[6]),
dr[3].ToString(),
dr[4].ToString(),
dr[5].ToString()
);
}
Could someone help me to figure out what I'm doeing wrong? I can't find it. This is the first time I use an DataGridView with comboboxes.
in my experience I found everything seemed to work better if you tied it in through a binding scource, then set the
bindingScource.dataScource.Rows.Add(
dr[0].ToString(),
dr[1].ToString(),
dr[2].ToString(),
Convert.ToInt32(dr[6]),
dr[3].ToString(),
dr[4].ToString(),
dr[5].ToString()
);
Select the correct row? you mean select from the dropdown to see the row inside the datagrid?
int index = dropdown.SelectedIndex();
for(int count = 0; count < dgvEenheden.Rows.Count; count ++)
{
if (dgvEenheden.Rows[count].Cells["<enter col name here>"].Value.ToString().equals(dropdown.Items[index].Text))
{
dgvEenheden.Rows[count].Selected = true; //to select the Row
dgvEenheden.Rows[count].Cells[<Cell Number>].Selected = true; //to select the specific Cell
}
}
Related
Hellou to everyone.
I would love the assistance from you guys.
My problem is like this:
I have this datagridview which I fill using datasource (a query to a db table). I only created manually one column which contain a checkbox. When the binding is complete I can click the checkboxes to select the row and I can select multiples rows this way.
I want to put the selected rows into a datatable (so I can read it and do some sql queries with the information on it) BUT when I read the selected rows to put them inside a datatable I need to skip the checkbox column (the first one).
How can I achieve this?
Okay, for future reference, I'm gonna post here how I fixed it.
DataTable dt = new DataTable();
if (dgvPedidos.SelectedRows.Count > 0)
{
foreach (DataGridViewColumn column in dgvPedidos.Columns)
dt.Columns.Add();
int i = 0;
foreach(DataGridViewRow row in dgvPedidos.Rows)
{
if (Convert.ToBoolean(dgvPedidos.Rows[i].Cells["chkPedido"].Value) == true)
{
dt.Rows.Add();
dt.Rows[i][1] = row.Cells[1].Value.ToString();
dt.Rows[i][2] = row.Cells[2].Value.ToString();
dt.Rows[i][3] = row.Cells[3].Value.ToString();
dt.Rows[i][4] = row.Cells[4].Value.ToString();
dt.Rows[i][5] = row.Cells[5].Value.ToString();
dt.Rows[i][6] = row.Cells[6].Value.ToString();
dt.Rows[i][7] = row.Cells[7].Value.ToString();
dt.Rows[i][8] = row.Cells[8].Value.ToString();
dt.Rows[i][9] = row.Cells[9].Value.ToString();
i++;
}
}
dt.Columns.RemoveAt(0);
I pull the data from sql database and fill datagridView1 with it:
dataGridView1.DataSource = bsour;
User will be able to edit value in 1 column, but possible values for that column are only Null, 0, 1 or 2 so to prevent from inputting incorrect value i decided to do a ComboBoxColumn:
DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn();
col1.Name = "il_normal_combo";
col1.HeaderText = "Normal";
col1.Items.Add("");
col1.Items.Add("0");
col1.Items.Add("1");
col1.Items.Add("2");
dataGridView1.Columns.Insert(5, col1);
The other now unnecessary column I'll hide with .Visible = false;
I want the ComboBoxColumn to display database values from that other column that will be hidden and i can't seem to get it to work.
What i have tried:
col1.ValueMember = "il_normal";
col1.DisplayMember = "il_normal";
Those don't work but without error message.
This doesn't work and produces error "Values are incorrect"
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Cells[5].Value = dataGridView1.Rows[i].Cells[7].Value;
}
I found similar question here
Change DataGridViewColumn to DataGridViewComboBoxColumn in bound DataGridView
but solution in that post didn't work for me. In ComboBoxColumn instead of number, I got System.Data.DataRows.(something) so i deduced that it doesn't know which column to use (even though there was only one in DataSource) so i added
col1.DisplayMember = "number";
And then it worked :-)
I have a DataGridView (CurrentLine) with a DataSet source table. I have added a combobox column with a different datasource, which works fine. The I tried adding another column, but the DataGridView refuses to show it!
I have tried adding a regular column via DataGridViewColumn and via DataGridViewTextBoxColumn (as I basically need a regular column with nothing but numbers).
I tried setting the visibility property to true.
Nothing seems to work and I have no idea why.
da1.Fill(Orders, Order);
da2.Fill(Orders, Lines);
da3.Fill(Orders, Products);
CurrentLine.DataSource = Orders.Tables[Lines];
CurrentLine.Columns["fkPrdctnum"].Visible = false;
CurrentLine.Columns["linenum"].Width = 60;
CurrentLine.Columns["linenum"].HeaderText = "Number";
CurrentLine.Columns["linenum"].ReadOnly = true;
CurrentLine.Columns["lineQuantity"].HeaderText = "Quantity";
CurrentLine.Columns["lineQuantity"].Width = 70;
CurrentLine.Columns["linePrdctPrice"].HeaderText = "Unit Price";
CurrentLine.Columns["linePrdctPrice"].Width = 100;
DataGridViewComboBoxColumn prod = new DataGridViewComboBoxColumn();
prod.DataSource = Orders.Tables[Products];
prod.DisplayMember = "prdctName";
prod.ValueMember = "prdctNum";
prod.HeaderText = "Product";
CurrentLine.Columns.Add(prod);
for (int i=0; i<CurrentLine.Rows.Count; i++)
{
int product = Convert.ToInt32(Orders.Tables[Lines].Rows[i][1]); //This loop sets the values of the combobox according to the order lines in the orderline table in the dataset.
CurrentLine.Rows[i].Cells[4].Value = product;
}
DataGridViewColumn LinePrice = new DataGridViewColumn();
LinePrice.HeaderText = "Line Price";
LinePrice.Width = 100;
LinePrice.ReadOnly = true;
CurrentLine.Columns.Add(LinePrice);
Just to be clear: the combobox works fine, and the loop to set its values also works fine. The problem seems to be somewhere with the DataGridViewColumn LinePrice.
Any idea what's going on? Why does one type of column (combobox) works fine but the other does not?
p.s. I have cut lines of code regarding another DataGridView which fills with the Order table, as that one works fine and is irrelevant to the problem so far as I can tell).
UPDATE: When I move the box to before the combobox column, it suddenly appears... Not sure why though, and it's not where I want it to be in the DataGridView.
Change your code as :
DataTable Orders = Orders.Tables[Lines];
CurrentLine.DataSource = Orders;
for (int i = 0; i < CurrentLine.Rows.Count-1; i++)
{
string product =Orders.Rows[i][1].ToString (); //This loop sets the values of the combobox according to the order lines in the orderline table in the dataset.
CurrentLine.Rows[i].Cells[4].Value = product;
}
DataColumn LinePrice = new DataColumn("Line Price");
Orders.Columns.Add(LinePrice);
It should be works
I made use of your code and I encountered the below error during the execution of code fragment which adds "Line Price" column.
Column cannot be added because its CellType property is null.
The above error can be generated if columns are added after you have already added rows into the DataGridView since it is not possible determine the cell type.
I managed to display the column based on the order you requested using the below code fragment. Please try to add in the new line and confirm if this helps to resolve your issue.
DataGridViewColumn LinePrice = new DataGridViewColumn();
LinePrice.HeaderText = "Line Price";
LinePrice.Width = 100;
LinePrice.ReadOnly = true;
LinePrice.CellTemplate = new DataGridViewTextBoxCell(); // Define the cell type of the new column
CurrentLine.Columns.Add(LinePrice);
NOTE: The for loop can generate error (#o_weisman also mentioned this) while making use of i to get hold of the Orders.Rows[i] row. Further, it seems that you have a master try catch block which is suppressing the errors. Please review your code to ensure that you have implemented error traps accordingly.
I'm Working on a windows form GUI that has a dataGridView. In dataGridView I've two columns. The first Column Type is a ComboBoxColumn for Insurance id and name and second one is Text box for insurance code number.
ComboBox Column's Datasource is already set and works.
here is code on form load :
DataGridViewComboBoxColumn Cb = new DataGridViewComboBoxColumn();
DataTable dt = new DataTable();
dt = _clsT.Fill_In_DataTable("SELECT insuranceId, insuranceName FROM insurance", false);
Cb.DataSource = dt;
Cb.Name = "insurance";
Cb.DisplayMember = "insuranceName";
Cb.ValueMember = "insuranceId";
dgv.Columns.Insert(0, Cb);
dgv.Refresh();
after loading form i can fill dgv(DataGridView) Rows From combobox items and at last i want get all row's information with clicking on a button.
here is my code for getting rows data on btn click event
if (dgv.Rows.Count > 0)
{
for (int i = 0; i < dgv.Rows.Count; i++)
{
lst1.Items.Add(dgv.Rows[i].Cells[0].Value.ToString());
lst2.Items.Add(dgv.Rows[i].Cells[1].Value.ToString());
//lst3.Items.Add(dgv.Rows.Cell[0] get displayMember.ToString() ) //what i must to do?
}
}
ok from here i can get insuranceId and insuranceCodeNumber but how to get displayMembers ?
please help me ... thanks
try this
dgv.Rows[i].Cells[0].FormattedValue.ToString();
dgv.Rows[i].Cells[1].FormattedValue.ToString();
i am working with Desktop Application and i am trying to create a datatable from a datagridview. but i only want to add those rows when checkbox is selected or checked. currently i created a datatable from a whole datagridview like this.
DataTable tbl = (DataTable)dataGridView1.DataSource;
I have added a checkbox coloumn inside datagridview.. so please guide me how to add selected rows to a new datatable
Thanks.. Here is the new code Update
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
{
DataRow dr0 = tbl.NewRow();
dr0["ID"] = dataGridView1.Rows[i].Cells["ID"].Value;
dr0["BikeCC"] = dataGridView1.Rows[i].Cells["BikeCC"].Value.ToString();
dr0["ChassisModel"] = dataGridView1.Rows[i].Cells["ChassisModel"].Value.ToString();
dr0["BikeCC"] = dataGridView1.Rows[i].Cells["BikeCC"].Value.ToString();
dr0["ChassisModel"] = dataGridView1.Rows[i].Cells["Chassismodel"].Value.ToString();
tbl.Rows.Add(dr0);
}
}
objMdl.RecordTable = tbl;
This is the way i could find to add selected rows into a new datatable... it is lenghty but i am still facing little issues. which i will post in a bit for your look.
Suppose your checkbox column is named yourCheckBoxColumn:
DataTable tbl = ((DataTable)dataGridView1.DataSource).Clone();//Clone structure first
var rows = dataGridView1.Rows.OfType<DataGridViewRow>()
.Where(r=>Convert.ToBoolean(r.Cells["yourCheckBoxColumn"].Value))
.Select(r=>((DataRowView)r.DataBoundItem).Row);
foreach(var row in rows)
tbl.ImportRow(row);