DGV ComboBox Cell remain updateable - c#

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.

Related

DevExpress: Row handle in gridview and returns a single row

I can’t to get the Row Handle in the GridView by the index in the DataTable DataSource.
I need to pass the value at this comand: gridView3.FocusedRowHandle = ????here????;
in short, what I'm doing. I have two datagrids, the first one must show the row highlighted and filtered, when I select information in the second datagrid.
Image by Form
in this example, I selected a row in the second datagrid that contains the value "cod.uniforme" = 15.
So the line was highlighted in red in the first datagrid, but the course was pointed in the 5th row
private void FirstDataGrid_RowStyle(object sender, RowStyleEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
string CodUniforme = View.GetRowCellDisplayText(e.RowHandle, View.Columns["CODUNIFORME"]);
if (CodUniforme == txtValorGridView.Text)
{
e.Appearance.BackColor = Color.Salmon;
e.Appearance.BackColor2 = Color.SeaShell;
e.HighPriority = true;
}
}
}

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.

Creating combobox in different rows on button click..inside gridview?

i want to create combobox inside gridview on button click event...
each time when i cklick on button a combobox will be created in the next row ..means every time only row will change column will remains same...
private void Buttton_Click(object sender, Event e)
{
DataGridViewComboBoxCell CellColumn1, CellColumn2, CellColumn3;
dataGridView1.Columns.Add("Col1", "Column1");
dataGridView1.Columns.Add("Col2", "sanjeev");
//make row 1 at all columns into combobox cell
dataGridView1.Rows[j].Cells[0] = new DataGridViewComboBoxCell();
dataGridView1.Rows[j].Cells[1] = new DataGridViewComboBoxCell();
CellColumn1 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[j].Cells[0];
CellColumn2 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[j].Cells[1];
j++;
}
i tried this logic ..this will create combox inside gridview only at first button click but when i click the button again this is not working..
can any one help me out....
finally i got the answer...
private void BTN_ADD_Click(object sender, EventArgs e)
{ int j=0;
DataGridViewComboBoxCell ColumnItem2 = new DataGridViewComboBoxCell(); // create a combobox cell
ColumnItem2.ValueMember = "Display";
ColumnItem2.DisplayMember = "Display";
for(int i=0;i<10;i++)// it will add elements to a combox
{
ColumnItem2.Items.Add(i);
}
dataGridView1.Rows.Add(); // add row everytime to add a new combobox to the next row.
int columncount = dataGridView1.ColumnCount;// count no of coloumns
int rowcount = dataGridView1.RowCount;;// count no of rows
//i kept count no of columns and rows so that by rows and columns count u can specify to which row or you want to add combobox inside gridview
// below i hard coded the combobox row and column to 2,2 so it will add a combobox to 2nd row and 2nd column.
dataGridView1[2, 2] = ColumnItem2;
//dataGridView1[2, j] = ColumnItem2;
// general way of adding combox to second column and row is everytime added by one as i m incrementing j value every time to each time on button click it will add combox on next row on second column..same way you can specify row and column as per your need.
j++;
}
hope this will help others....
http://csharpprobsandsoln.blogspot.in/2013/04/how-to-add-combobox-dynamically-to.html

Datagridview comboboxes only in some rows

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.

first locating, then changing the value of a cell in a selected row in a datagridview?

Im trying figure out how I can change the value of a specific cell in a selected row of a datagridview. I am able to change the value but not in the highlighted row, only the first one. The hghlighting of the row is defined in the "find" button code.
For example: I enter a text, it search each row for that text in the specific column and then scrolls to and highlights/selects the entire row. (This Works Fine)
I then want to be able to get the value of a specific cell of the highlighted row by selecting the column name already present in the dropdownlist(combobox). It then gets the value of that cell and deducts from it a value present in a numericalUpDownCounter(nudQTY).
So far i can do this for different cells selected by column name (cbSupplList) but it only does it for the first row not the highlighted row defined by the "Find" button Code.
The following is my attempt so far:
private void btnFind_Click(object sender, EventArgs e)
{
/* Code to search the alphanumneric Part Number (in Column1 header called "PART NUMBER")
and highlihgt the row*/
foreach (DataGridViewRow row in dataGridView1.Rows)
{
// Removes the row selection
row.Selected = false;
var cellValue = row.Cells["PART NUMBER"].Value;
if (cellValue != null && cellValue.ToString() == tbPartNum.Text.ToUpper())
{
// Scrolls the found row up and highlights the entire row
row.Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;
}
}
}
private void btnSold_Click(object sender, EventArgs e)
{
int cellVal;
int newCellVal;
cellVal = Convert.ToInt32(dataGridView1.CurrentRow.Cells[cbSuppList.Text.ToString()].Value);
newCellVal = Convert.ToInt32(cellVal - nudQty.Value);
dataGridView1.CurrentRow.Cells[cbSuppList.Text.ToString()].Value = newCellVal;
}
It seems it may have to do with the SELECTED ROW in the "Find" code and the CURRENT ROW in the "Sold" Code. Making the SELECTED.ROW = CURRENT.ROW would solve it.... any ideas?
You are deselecting only Rows but no Cells.
dataGridView1.CurrentRow - "Gets the row containing current cell"
And Current Cell remains what it was = first cell of a first row.
In order to clear all selected rows/cells use
dataGridView1.ClearSelection()
And instead of
row.Selected = true;
use
row.Cells["PART NUMBER"].Selected = true;
As currently you are selecting only row but not the cell.
I hope this helps...
Otherwise you might want to find relevant cell in btnSold_Click() the same way you did in btnFind_Click()
.
.
EDIT
complete solution for you
private void btnSold_Click(object sender, EventArgs e)
{
int cellVal;
int newCellVal;
cellVal = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[cbSuppList.Text.ToString()].Value);
newCellVal = Convert.ToInt32(cellVal - nudQty.Value);
dataGridView1.SelectedRows[0].Cells[cbSuppList.Text.ToString()].Value = newCellVal;
}
I've just replaced CurrentRow with SelectedRows[0] and works fine.

Categories