Datagridview comboboxes only in some rows - c#

In my winforms application, I have a DataGridView where one row uses the DataGridViewComboBoxColumn control so that it contains comboboxes for this column.
Is it possible to programatically replace some of the comboboxes in that column with something else? (for example a label saying "n/a"). I only want a combobox in certain rows.

You should use DataGridViewComboBoxCell property instead of DataGridViewComboBoxColumn property. Something like below:
for(int intCount=0;intCount<dgv.Rows.Count;intCount++)
{
if(intCount % 2 == 0) //Your own condition here
{
DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell();
//cmb.Value // assign values in the combobox
dgv[0,intCount] = cmb;
}
else
{
DataGridViewTextBoxCell txt = new DataGridViewTextBoxCell();
txt.Value = "n/a";
dgv[0,intCount] = txt;
}
}
In the above example, I am assigning the Individual cell properties in the first column.

Related

How to change the color of a DataGridView-Cell containing a specific value [duplicate]

I have a DataGridView. I set its .DataSource prop to be an BindingList of my own objects: a BindingList<IChessItem>
I then created some columns for it..
DataGridViewTextBoxColumn descColumn = new DataGridViewTextBoxColumn();
descColumn.DataPropertyName = "Description";
descColumn.HeaderText = "Description";
descColumn.Width = 300;
DataGridViewTextBoxColumn gameIDColumn = new DataGridViewTextBoxColumn();
gameIDColumn.DataPropertyName = "GameID";
gameIDColumn.HeaderText = "Game ID";
gameIDColumn.Width = 60;
dataGrid.Columns.Add(descColumn);
dataGrid.Columns.Add(gameIDColumn);
My question is.. I want to color one of the columns GREEN based upon data in another field of my BindingList). How can I do this?
I don't really have to show this field, I just want to act upon the data in it..
in my case, one of the fields of IChessItem shows whether the record is new, and I want to color the other fields in the datagridview to reflect that.
You can use the 'CellFormatting' event of the DataGridView. The DataGridViewCellFormattingEventArgs contains indexes of the row and the column of the current cell as it is being bound. I hope my code example makes some sense to you:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// Compare the column to the column you want to format
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "ColumnName")
{
//get the IChessitem you are currently binding, using the index of the current row to access the datasource
IChessItem item = sourceList[e.RowIndex];
//check the condition
if (item == condition)
{
e.CellStyle.BackColor = Color.Green;
}
}
}
You may populate data in your DataGridView using any loop or datasource. Then for each DataGridViewRow in DataGridView1.Rows----
Chk the ref value ypu want to chk and then set DataGridviewCell[index].style.backColor property.

C# DataGridView with ComboBox always showing the combobox?

So I have a DataGridView with one column set to type ComboBox. When I add a new row, I populate the combobox for the cell like so:
foreach (Job cJob in cJobs)
{
cbItem = new ComboboxItem();
cbItem.Text = cJob.Name;
cbItem.Value = cJob.Name;
Object oCell = dgItem.Cells[1];
oComboxCell = (DataGridViewComboBoxCell)oCell;
oComboxCell.Items.Add(cbItem);
}
I then set the value of the cell as normal:
dgItem.Cells[ColumnIndexes.IND_ENTRIES_JOB].Value = cboJob.SelectedItem;
For all intents and purposes the value is indeed set to what I set it to, but on the screen the combo box is visible on top and the first item in the combo is what's displayed as the value! What could be causing this? I'm sure it's worked in the past.

DataGridViewComboBoxColumn not populating values from the data source

I have datagridview in a tabpage. the datagridview has 6 columns. The 6th column is a comboboxcolumn. I am trying to bind a datasource to the comboboxcells. Each row would have different datasource based on the row number. The datagridview will have 10 rows always. The problem is the comboboxes are not populating any values. It just gives me blank item. Both datagrid and comboboxcolumn datasources showing data table values when if I keep break points.Could someone tell me what I am missing here?
private void BuildFreshAccessMatrix()
{
dataGridView1.AutoGenerateColumns = false;
DataGridViewComboBoxColumn cboPermissionCol = (DataGridViewComboBoxColumn)dataGridView1.Columns[5];
//cboPermissionCol.DataPropertyName = "UserLevelCategoryName";
dataGridView1.DataSource = dataProvider.GetBlankMatrixData();
int i = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewComboBoxCell cboPermission = (DataGridViewComboBoxCell)(row.Cells["UserLevelCategoryNameAdd"]);
cboPermission.DataSource = dataProvider.GetPermissionComboData(i);
cboPermission.DisplayMember = "UserLevelCategoryName";
cboPermission.ValueMember = "UserLevelCategoryName";
i += 1;
}
}
Try to change your code to :
DataGridViewComboBoxCell cboPermission = (DataGridViewComboBoxCell)
dataGridView1.Rows[i].Cells["UserLevelCategoryNameAdd"];
instead of :
DataGridViewComboBoxCell cboPermission = (DataGridViewComboBoxCell)
(row.Cells["UserLevelCategoryNameAdd"]);

How to get Display Member of All Combobox columns in dataGridView in C#?

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

DGV ComboBox Cell remain updateable

I have a DataGridView that I want users to be able to add records to directly. This is done by clicking a link beneath the DGV, at which point a new row is programmatically added, and the first visible cell is of ComboBoxCell type. Code excerpt for how I'm doing this is:
// Add a new row to DGV
DataGridView dgv = this.dgvInformation;
DataGridViewRow newRow = new DataGridViewRow();
dgv.Rows.Add(newRow);
// Create cells and add to row
DataGridViewComboBoxCell cellInfoType = new DataGridViewComboBoxCell();
newRow.Cells["InfoType"] = cellInfoType;
// Create DataSource based off LINQ query here
List<ComboItemAccountInfoType> comboDataSource = new List<ComboItemAccountInfoType>();
// List is populated here
// Assign DataSource to combo cell
cellInfoType.DataSource = comboDataSource;
cellInfoType.ValueMember = "AccInfoTypeID";
cellInfoType.DisplayMember = "InfoType";
// Scroll new row into view and begin editing
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
dgv.CurrentCell = dgv[1, dgv.Rows.Count - 1];
dgv.BeginEdit(true);
The DataSource contains some values with an ID of -1, which are categories that the user should not be able to select, and all other values have their valid database ID. This all works fine, except that if a user has selected a -1 row I am unable to keep the combo cell in edit more, as it simply stores the value in the cell and I can no longer activate the drop-down. I have added the following code to the _CellValueChanged event, but it has no effect:
private void dgvInformation_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = this.dgvInformation;
DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex];
// If type of cell is ComboBox then
if (cell.GetType() == typeof(DataGridViewComboBoxCell))
{
DataGridViewComboBoxCell cellInfoType = (DataGridViewComboBoxCell)cell;
if ((int)cellInfoType.Value == -1)
{
MessageBox.Show("Going back into edit mode...");
dgv.CurrentCell = cell;
dgv.BeginEdit(true);
}
else
{
MessageBox.Show(cellInfoType.Value.ToString());
}
}
}
I do get the "Going back into edit mode..." message here after moving onto another cell, but it doesn't then do what it says! Could anyone please explain why it won't go back into edit mode, or is there a way of preventing the value becoming locked as soon as it has been selected?
Many thanks!
There should be a "validating" event for you to intercept and cancel if the value is -1.

Categories