I have a Datagridview which is showing some records from DB. Now I want to add a dropdown menu in between the columns of this Datagridview.
How could I achieve that? My DGrid is bound in runtime so I am not initializing every column here!
Help?
Use below code to add your Combobox Column at specific index of DataGridView
DataGridViewComboBoxColumn myCombo=new DataGridViewComboBoxColumn();
myCombo.HeaderText = "My Combo";
myCombo.Name = "myCombo";
this.dataGridView1.Columns.Insert(n, myCombo); // n is index
Related
I have a dgv with 3 columns, ProductID | ProductDesc | CurrentDiscount.
Currently, the 3 columns are textboxcolumns, are are populated using a datasource binding to a SQL table which works fine.
What I actually want is for the CurrentDiscount column to be a ComboBoxColumn which when the dgv is loaded, it populates the cell (on each row) with the current data from the database table, but allows me to use the ComboBox feature to change the discount value to something else.
I have been searching for hours and cannot figure out how to do this.
Any help would be appreciated.
P.S, there is no point me sharing current code as its all programmatically generated (and is HUGE) when I create the dgv and use a wizard to assign it a datasource.
Using a DataGridViewComboBoxColumn is very straightforward if your selection list is the same for every row.
Here's an example, assuming discountTbl is a DataTable containing the list of discounts and has columns DiscountId and DiscountName:
DataGridViewComboBoxColumn grdCol = new DataGridViewComboBoxColumn();
grdCol.DataSource = discountTbl;
grdCol.DataPropertyName = "DiscountId";
grdCol.DisplayMember = "DiscountName";
grdCol.HeaderText = "Discount";
grdCol.Name = "DiscountId";
grdCol.ValueType = typeof(System.Int32);
dgv.Columns.Add(grdCol);
I have a DataGridView (AllowUserToAddRows = true, EditMode = EditOnKeystrokeOrF2) bound to a BindingSource (bound to a DataTable).
My DataGridView has a ComboBoxColumn where I can select countries.
The DataGridView has AllowUserToAddRows on true because the user should be able to add rows by selecting a country from the blank "new row" that every DataGridView has.
The problem is that whenever I click on the empty row with my mouse it adds the new row to my BindingSource. However, the new "blank" row for the DataGridView doesn't appear until you actually select a country from the former (clicked) new row.
Therefore, the user doesn't know he added a new row. And the row has DBNull.Value as Country because nothing was selected. When trying to save, it throws an error because the Country can't be null.
So I need to prevent the new row from being added to the BindingSource before a value is selected from the ComboboxColumn.
I tried RowValidating-Event to cancel the edit when Country is DBNull, but it prevents me from leaving the new row. That is not what I want.
Basically, I think I have this problem, without the "closing form" part.
The 'Blank' row that exists in the data grid view is there just for the addition of new rows, it doesn't exist in the datasource, until you actually click there. Once you make an edit to that 'new row' area and move off the row, the datagridview will add another 'new row' area. My suggestion would be to add some validation before save and exclude any rows failing validation.
Making the DataGridView lose focus before I called my Save method (which calls Update on my DataAdapter) made the empty row disappear from my BindingSource.
i have desktop application in which i am using data gridview control. i want to use a combo-box in each grid row that holds 2 items (Sale, Return). i am as far as single row concerns, i am not getting any issue but when i enter any other row, it shows me double comboboxes in each row and tripple for third row and so on.
second issue that i am facing is that i am not getting both of two items that i am trying to insert through items.add method. below is code that i am using to insert combobox column in grid
custCartGrid.Rows.Add();
GridRow = custCartGrid.Rows.Count-1;
DataGridViewComboBoxColumn dcom = new DataGridViewComboBoxColumn();
dcom.HeaderText = "Combobox";
dcom.Items.Add("Sale");
dcom.Items.Add("Return");
custCartGrid.Columns.Add(dcom);
please help me in this..
My aim is simple. Assume I have 5 Lines of data which I filled in DataGridView. In 1st Column I filled data as shown below. User should fill the data in 2nd Column.
Name: --
Age: --
District: --
D.O.B: --
State: --
Now I have to place ComboBoxes in 2nd Column of DataGridView for "District" and "State" rows only and leave the remaining rows editable. With DataGridView I can make all Rows either ComboBox or TextBox. I googled for this but of no use. I tried 3rd Party tools also but this type of implementation I didn't find. I tried to take ListView/TreeView instead of DataGridView but I did not achieved. I am using C# Winforms VS2010.
If given any idea I am ready to start from scratch. Thanks in advance.
EDIT:
Below is the output where the highlighted "Rows" should be "ComboBox" and remaining should be "Textboxes".
If I add the below code whole Column is changing to ComboBox.
DataGridViewComboBoxColumn districtColumn = new DataGridViewComboBoxColumn();
districtColumn.DataPropertyName = "container";
districtColumn.HeaderText = "Details";
districtColumn.ValueType = typeof(Container);
districtColumn.DisplayMember = "District";
districtColumn.ValueMember = "code";
districtColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
dataGridView1.Columns.Add(districtColumn);
Hope now You clearly understand my Problem.
Thanks..
Ahh... I got you this time.
What you need is DataGridViewComboBoxCell, create a comboxcell and place it in grid view cell.
DataGridViewComboBoxCell cmbcell = new DataGridViewComboBoxCell();
cmbcell.DataSource = Districts; // Your data source
DataGridViewCell normalcell = new DataGridViewTextBoxCell();
normalcell.Value = "Name"; // creating the text cell
dataGridView1.Rows[iRowIndex].Cells[1] = cmbcell; // use your own logic to set row index
dataGridView1.Rows[iRowIndex].Cells[1] = normalcell;
Hope this helps !
well i have worked with datagridview now i need to work with a listview i dont know how to select the value it has in one cell..
my another option is when a cell was added or deleted, all this information is on datagridview
but it doesn't have a (datasourcechanged) then how can i anyone of these?
I tried secund with this code:
DataGridView1.DataSource = lvDevices
so i could select anyone but i need when in datagrid changed its value, it passed to datagridview well my another way is
how can i select the value of one cell in a ListView?
you could try this in this listview1_doubleclick event. ...
string a = listView1.SelectedItems[0].SubItems[column index corresponding selected cell].Text.