How to fill only Selected cells of GridView with Combobox - c#

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 !

Related

Populate ComboBox starting value from datagridview dataset

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);

Binding user data on button press

I don't know a good way to explain this but what I have so far is I created a dataset and called it Database and created a table in it. Then I used a bindingsource and defined Database as its datasource.
Then I put the bindingsource as my datagrids data source and I put my textboxs text to the respective column in the bindingsource (e.g. textbox1 text=bindingsource - Name) so I can update my xml so I can click on the row and it fills the textboxes with the rows info and as i edit the text box it changes the data in the rows on the grid view so far an edit and update.
But I want to be able to have the option to update or not. So instead of I type and it changes I want to be able to fill in the boxes then choose to press update or cancel like a normal edit form.
This is my form
It's as if I could do something like
string = textboxname
then when I press update it then does selected row column "name" = string
ok so i figured it out thanks to devTimmy. i was looking in the wrong place and with the wrong ideas.
iv now done it so it takes the selected row index and uses that to fill in the correct row when its told which cell to fill. i didn't think to tell it which cell before so it failed and i thought no more of it.
here is my code.
int i;
i = dataGridView1.CurrentRow.Index;
dataGridView1.Rows[i].Cells[0].Value = Nametb.Text;
dataGridView1.Rows[i].Cells[1].Value = Locationtb.Text;
dataGridView1.Rows[i].Cells[2].Value = Infotb.Text;
dataGridView1.Rows[i].Cells[3].Value = dayvisittb.Text;
database1.WriteXml("Database.xml");

Datagridview rows are 0 even though I have a datasource

I am programmatically creating a datagrid view and binding data to it. For some reason the rows are empty. Strange as it may seem but I do not want to display the Datagrid view just pass it into a class which extracts the data as Excel and CSV.
this.DataGridView = new System.Windows.Forms.DataGridView();
System.Windows.Forms.BindingSource bindingSource = new System.Windows.Forms.BindingSource();
bindingSource.DataSource = DataSource;
this.DataGridView.AutoGenerateColumns = autoGenerateColumns;
this.DataGridView.DataSource = bindingSource;
Int32 rowCount = this.DataGridView.Rows.Count; // 0!
Is there any way after creating the data source I can get the rows to populate?
Thanks
I believe DataGridViews don't bind/contain any rows until they are actually rendered.
#paul.abbott.wa.us is right.
You can move the code that count the rows in DataGridView.DataBindingComplete Event
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.databindingcomplete.aspx
I wonder why you need a DataGridView without displaying it on the UI in the first place. There are couple of alternates I can suggest
Let go the DataGridView and use DataTable/IEnumerable to keep the data. Both can provide you with
counts.
If you insist on keeping the DataGridView then something like
(Assuming its bound to a DataSet)
((DataSet)dataGridView1.DataSource).Tables[0].Rows.Count
would also work.
In order to see data grid, you should add it to parent controls collection:
DataGridView = new System.Windows.Forms.DataGridView();
Controls.Add(DataGridView);
Currently your grid do not have any parent and cannot be displayed.

How to add a single combobox into a datagridview in random positions

I want to add DatatGridViewComboBoxCell into dynamically determined cells onto the datagridview.
So far all of the examples I have found either make the entire columns' cells into comboboxes or don't work.
Here is the simplest example that I can come up with. I have tried databinding to sources which work to a point. The items exist in the combobox.items but on the datagridview the combobox is empty and you cannot select a value
DataGridArticles.Columns.Add("columna", "columna")
Dim combo As New DataGridViewComboBoxCell
combo.Items.Add("b")
combo.Items.Add("ba")
combo.Items.Add("ca")
DataGridArticles.Rows.Add()
DataGridArticles.Rows(0).Cells(0) = combo
Is it possible to add a combobox to a specific cell in a datagridview.
I've never had any problems with DataBound DataGridViewComboBoxCell.
I'm using it like this:
Dim cell As New DataGridViewComboBoxCell()
cell.DisplayMember = "Name"
cell.ValueMember = "Id"
cell.DataSource = list
DataGridArticles.Rows(0).Cells(0) = cell
cell.Value = 0 //It will select and display item with Id = 0, if you do not set it
//then combobox will look exactly like yours on image posted (like
//there is no items in it).
It worked flawlessly every time I used it. So if it does not work for you may.
So to fix your problem add this line after adding items to combobox:
combo.Value = "b";
Hope it helps :)

Adding a dropdown box in Datagridview

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

Categories