bind DataGridViewComboBoxColumn in C# from DB - c#

I had a column in my DataGridView which is a DataGridViewComboboxColumn, and I need to bind this column from a database table which is also has a column with values like 1,2,3. Based on these values the combo box must display different values, ie. if 1 "Manager" and so on.

Let's use Northwind database as our test data.
These are the steps:
Configure datasource.
Drag a DataGridView to the form.
Drag Products onto the DatagridView.
Right click on DatagridView - Edit Columns.
Select CategoryID, change column type to DataGridViewComboBoxColumn.
Choose the datasource(Category table), display member(CategoryName) and value member(CategoryID) for the column. Click OK to apply changes.
Build and run the project. Voila~

Related

DataGridViewComboBoxCell not updating list item

I'm trying to update values in a combobox in my datagridview. Sounds simple enough right.
I have several pre-existing rows added into my DGV where 2 columns are dropdown lists.
Code snippet as below, but the list is never updated visually with the new category.
This method works for other stand alone combo boxes, but not for the ones in the datagridview.
// List<string> _mCategories
_mCategories.Add("new category");
_mCategories.Sort();
for(int i = 0; i < dgv.Rows.Count; i++)
{
DataGridViewComboBoxCell c1 = dgv.Rows[i].Cells[4] as
DataGridViewComboBoxCell;
c1.Items.Clear();
c1.Items.AddRange(_mCategory.ToArray());
}
I should have given some more info on this than just a a scrappy comment
In a data binding scenario I would:
Create a new dataset
Add a table to it to hold all the data you want to show in the grid - example Address. Add tow int columns to it for a lookup, e.g. AddressType where 1 will be Work and 2 will be Home, and BuildingType where 1 will be "Apartment Block" etc...
Add two more tables with two columns each - one column holds the text string to show in the combobox lists (e.g. "Apartment Block" in the building type combo or "Home" in the address type combo), the other column holds the number (1) you want to store in the main table. I tend to call these columns Disp and Val because the DisplayMember property of the combo will be set to the text Disp column and the combo's ValueMember will be set to Val.
Save the dataset
Open the data sources window in VS
Expand the dataset node, drag the node representing the main table (Address), onto the form
A datagridview appears and it is databound to the new dataset instance via the binding source (these components appear at the bottom of the form designer - see the somedataSet, and the addressBindingSource)
Edit the columns of the grid, change the AddressType column to be a datagridviewcombobox type
Set it's DataSource to be the table that holds the display/value pairs for the combo e.g. Other >> Project Data Sources >> DataSetName >> AddressType
Set the DisplayMember of the combo to be the string column (Disp)
Set the ValueMember of the combo to be the int column (Val)
Check that the DataPropertyName property is referencing the name of the column in the main grid that holds the addresstype (AddressType)
That should be it. Your combo will consult the dataset.AddressType table to get the list it shows, it will show the contents of Disp, and it will use the value it finds in Val to poke into the Address.AddressType or Address.BuildingType columns (depending which combo it is)
Add some data to your main table (don't add data to the grid; add it directly to the datatable the grid is bound to) and add a load of addresstypes/buildingtypes to the smaller tables
Now if your main table has a 1 in its addresstype column, the combo will show "work". If you change it to "home" the main table row will be updated to 2
I followed the steps here in a video and recorded it. At one point you'll see me drop a button on the form. That's just so I can have something to click that will hit a breakpoint then I can inspect the contents of the dataset and show you how it goes
https://youtu.be/EuHrIE3-BoE

Copying/Saving Datas to database from a drop down in Gridview

I have a Gridview which has 10 columns, one of which is a dropdown. I want to take the selected value from it and save it in a specific column of a database. I want to know how and where to access this dropdownlist. If there is a code it will be useful.

Datagridview row select values from record not showing

I have a datagridview that is populated by a combo box with 1 column. I need to be able to select that record and insert values not shown in the datagridview. Normally I would do this when I populate the all the values needed:
newlien.LienReason = (string)row.Cells["LienType"].Value;
newlien.LienNumber = (string)row.Cells["LienNumber"].Value;
Which will not work as the cells are not being populated. I am guessing that there are two solutions: either add the other columns and hide them and then pull the values from hidden cells or some way to get the values of the record without adding the values to the DGV. Either way I have no idea how to do it!
The easiest way to do this is to add the desired column(s) to your DGV and then set visible to false in those DGV column properties. Then you can still pull values from the hidden columns, just like you would from any other column:
//hidden column
newlien.AnotherVariable = (string)row.Cells["HiddenColumn"].Value;
Let me know if this helps.

How to populate and select data in comboboxcell in datagridview

Friends, I've a datagridview in my windows application. It has 6 columns, out of which the 2nd(colindex 1) is of type combobox. Others are of textbox type. I've to populate this combobox from table1. There are two columns in table1 - ID and Name. Only Name will be displayed in the combobox. User can select any one from that combobox and write something in other 5 columns. Upon pressing save button, the ID of the selectedItem from combobox and other 5 textboxes' value will be stored in table2. When again the form will be loaded it'll fetch data from table2 and display corresponding data in datagridview. I've used datasource to populate the combobox. But it's not working. Do any one of you have any idea, how can I do it?
Try Setting the DisplayMember of the combobox to "Name" and the ValueMember of the combobox to ID.
And Also the bindings (For that matter any changes) you perform at design time is in InitializeComponent method of your class, depending on the version of Visual Studio you're using, It'll reside in a same or separate file.

How to add a new row into datagridview using grid only. i.e by showing textboxes and add button in WinForm

I want to add a new row to a datagridview such that when a user clicks on new button , a new is is generated with few textboxes and combo boxes and after filling up all the details he save the info by clicking on save button.
EDIT
I want to do it like it is seen in gridview(Template Fields) in asp.net
I am looking for same kind of functionality.
Since I'm not sure exactly how you want to achieve this.. I would have a hidden panel with your text boxes, ...etc, show the panel when the new button is clicked. After all information is entered into the fields, click the save button. Assuming that you will be inserting this information into a table, after the row is inserted, call the stored procedure to get the desired records from the table being displayed in the grid.
Assuming your _dataGridView.Columns collection is not null and contains a template of the rows you wish to add, it is as simple as something like this:
foreach(var item in _collection)
{
_dataGridView.Rows.Add(item.Foo, item.Bar);
}
In order for this to work, you will have had to design your Columns collection in the VisualStudio designer or programatically add DataGridViewTextBoxColumn objects to the Columns collection.
In the example above, I added two DataGridViewTextBoxColumn objects to the _dataGridView.Columns collection and then populated the datagrid from a List of my object that contained a 'Foo' and a 'Bar'.
EDIT
Have you checked out the DataGridView FAQ? The information about using the DataGridView in unbound mode may help you.
HTH

Categories

Resources