Select Dropdown item in datagridview column - c#

I have to manually select the item from the dropdownlist that is in the datagridview column, but the issue is while selecting the item i need to click on the dropdownlist multiple times.
How to resolve this? Any help will be highly appreciated.
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Data";
cmb.Name = "cmb";
dgv2.Columns.Add(cmb);

Set EditMode property of the DataGridView to EditOnEnter: link
DataGridView.EditMode - Gets or sets a value indicating how to begin editing a cell.
EditOnEnter - Editing begins when the cell receives focus.

The below code must be tied into the CellClick event of the datagridview:
private void datagridview_CellClick(object sender, DataGridViewCellEventArgs e)
{
bool validRow = (e.RowIndex != -1); //Make sure the clicked row isn't the header.
var datagridview = sender as DataGridView;
// Check to make sure the cell clicked is the cell containing the combobox
if(datagridview.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && validRow)
{
datagridview.BeginEdit(true);
((ComboBox)datagridview.EditingControl).DroppedDown = true;
}
}
Try Setting EditMode property to EditOnEnter.
I hope this helps!

Related

how to modify checkbox which located in datagridView

I have datagridView inside of this datagridView. We have checkbox located.I can't make selected true or false this particular checkbox which located in datagridview.
I add checkbox inside of datagridview with following code
DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
check.HeaderText = "Юклаш";
check.Name = "assing";
this.dataGridView1.Columns.Add(check);
DataGridView has CellContentClick method which will work after clicking some of cells from dataGridView this looks following
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "assing")
{
int order_id = (int)dataGridView1.Rows[e.RowIndex].Cells["id1"].Value;
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells["assing"];
chk.Selected = true;
//chk.Selected = true dosn't work
}
}
Here is a question: I can't make this checkboxcell selected or not selected. How can I make it selected because chk.Selected = true don't work ?????

Set DataGridViewComboBox default equal to existing DataGridView column

I have added a DataGridViewComboBox to a bound DataGridView (grdBOOK), the DataGridViewComboBox will replace column 3 to allow for user selection. I'm struggling to set the default of the DataGridViewComboBox equal to the value of column 3 so user selection is not required if the value is correct.
I pulled the code below from the net, but I get an error:
DataGridViewComboBoxCell value is not valid.
I thought a ComboBox cell could be treated as a normal DataGridView cell, but (see code below) an error is generated when a string is added to the ComboBox column? I've trawled the net and SO for a few days but nothing works, any suggestions please?
public void BOOK_COMBO2()
{
DataGridViewComboBoxCell cb_cell = new DataGridViewComboBoxCell();
DataGridViewComboBoxColumn cb_col = new DataGridViewComboBoxColumn();
// Contract field
cb_col.Items.AddRange("YEARLY", "MONTHLY", "");
cb_col.FlatStyle = FlatStyle.Flat;
cb_col.HeaderText = "newCONTRACT";
cb_col.Width = 50;
cb_col.ValueType = typeof(string);
// Add ComboBox and test
grdBOOK.Columns.Insert(5, cb_col);
grdBOOK.Rows[14].Cells[4].Value = "zzz"; // No error adding string to normal dgv column
grdBOOK.Rows[14].Cells[5].Value = "xxx"; // Error adding string to dgvcombobx column
//copy old values to new combobox and set as default
foreach (DataGridViewRow item in grdBOOK.Rows)
{
item.Cells[5].Value = item.Cells[3].Value;
}
//hide original column
grdBOOK.Columns[3].Visible = false;
}
After more research on the net, IMHO using a ContextMenuStrip is a better method of achieving this. Link here. A ContextMenuStrip has better methods, events, properties etc. I hope this helps others looking for a solution.
private void dataGridView1_DataError(object sender,
DataGridViewDataErrorEventArgs e)
{
// If the data source raises an exception when a cell value is
// commited, display an error message.
if (e.Exception != null &&
e.Context == DataGridViewDataErrorContexts.Commit)
{
MessageBox.Show("");
}
}
private void Form1_Load(object sender, EventArgs e)
{ dataGridView1.DataError +=
dataGridView1_DataError;}

On editing event , how to check if in the dropdownlist cell was selected a value

I have a gridview , with the following columns:
I've transformed the column cell into a dropdownlist via the edit template and added 2 items : Yes and No
NAME|AGE|Birthday|Code
Joh 21 12.12.2 Yes/No
there are 2 columns set = false; on page load.
I want to check on the row editing event from the gridview if the dropdownlist value == Yes then the 2 columns.visible = true;
I don't know how to do do the check thing..:(
Thank you
After discussing it, we seem to have come up with this answer, using an OnSelectedIndexChanging event from the Yes/No DropDownList, instead of the Row Editing Event:
GridData is the ID of the GridView itself.
protected void OnSelectedIndexChanging(object sender, EventArgs e)
{
DropDownList id = (DropDownList)sender;
GridViewRow row = GridData.Rows[GridData.EditIndex];
if(id.SelectedValue == "Yes")
{
TextBox column1 = (TextBox)row.FindControl("Column1ID");
column1.Visible = true;
TextBox column2 = (TextBox)row.FindControl("Column2ID");
column2.Visible = true;
}
}

Changing DataGridViewButtonColumn's Button Text Per Row

I have a DataGridView with one column of type DataGridViewButtonColumn.
I want to put a text on the button. I've tried in the Edit Column options to put the text in the Text properties. I have even tried swtteing UseColumnTextForButtonValue = true.
I can't find a way to make it work.
Did you want to bind DataGridViewButtonColumn Text to the DataGridView's DataSource? If so then set the DataPropertyName for the column via the Edit Column options.
Otherwise you can try using the DataGridView's CellFormatting event:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Value = "some value";
}
}
Assuming that your DataGridView name is DataGridView1
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCell cell = row.Cells[0]; //Column Index for the dataGridViewButtonColumn
cell.Value = "Your Text";
}

How do I add button in each row of column of already bound datagridview in Windows application?

I want to add a button to each row of a column of an already bounded datagridview, and add an event to it (in VS 2005, Windows application).
I have searched a lot but was unable to find a working solution.
before binding to the datasource set :
grd.AutoGenerateColumns = false;
create yourself all DataGridView columns and bind them to the datasource:
DataGridViewTextBoxColumn dgvc = new DataGridViewTextBoxColumn();
dgvc.HeaderText = "column_header";
dgvc.DataPropertyName = "column_name";
create a DataGridViewButtonColumn.
DataGridViewButtonColumn dgvbt = new DataGridViewButtonColumn();
If you want this column not bound, set header text, the same text on all buttons:
dgvbt.HeaderText = "OK?";
dgvbt.Text = "ok"; // works also when bound
dgvbt.UseColumnTextForButtonValue = true; //
If you want your column to be also bounded and each button have the text of underlying cell, bind it:
dgvbt.DataPropertyName = "column_bt";
Add created columns to the DataGridView:
grd.Columns.Add(dgvc);
grd.Columns.Add(dgvbt);
handle the CellClick event of the DataGridView:
grd.CellClick += new DataGridViewCellEventHandler(grd_CellClick);
void grd_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex == index_of_button_column)
{
MessageBox.Show(this, e.RowIndex.ToString() + " Clicked!");
//...
}
}
for more, see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncolumn.aspx

Categories

Resources