I have a DataGridView (dgvTable) with 2 columns. The first column contains combo boxes with preset data from a database. The second column is just text.
I am trying to set the selection of the combo boxes in the first column based on the information in the second.
I simplified my code a bit to what I need help with:
string data = "MATCH THIS VARIABLE";
foreach (DataGridViewRow row in dgvTable.Rows)
{
if (match.Equals(row.Cells[1].Value.ToString())) //checking to see if the second column value matches data
{
row.Cells[0].Value = "HELP"; //if the second column value == data then set the combobox selected value to "HELP"
}
}
The combo boxes doesn't show the value
EDIT:
I started a new project and made a datagridview and added a columncombobox. I did set the value of the combobox successfully. The only difference is I am trying to change the datagridview combobox value in a separate window. Would that change anything?
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlName");
ddl.SelectedValue = "HELP";
Related
I want to read the the combobox column as shown in the picture, i've tried using
string mode = dt.Rows[0]["Mode"].ToString();
But it doesn't work, it read the USID column instead.
How to read the combobox value?
datatable
private void getmode()
{
//from here
var modelist = new List<string>() { "Reg0", "Basic", "Extended" };
dgvmode.DataSource = modelist;
dgvmode.HeaderText = "Mode";
dtDataGridView.Columns.Add(dgvmode);
// until here, i've create a combobox column in my datatable
string mode = dt.Rows[i]["Mode"].ToString(); // this line got error, it can't read the "Mode" column and say it is not exist, but in my picture, it is exist as a combobox column.
Messagebox.Show(mode); // here to show i get the combobox value
}
datatable is unable to reada datagridview feature, thus, when i using
yourdatatable.Rows[i]["Mode"].ToString();
it will comes out an error (something likie "Mode" column doesnt exist)
even you change the "Mode" to an integer that indicate the column you want to read, it will automatically read the column that create by datatable only. For example :
yourdatatable.Rows[i][0].ToString();
it wont read the combobox column and it will read normal column instead (will not read combobox column ("Mode"), which is the first column. But it will read the column ("USID"), which is the second column).
then, i need to use datagridview like the code below to read the combobox value:
yourDataGridView.Rows[i].Cells[0].Value.ToString()
I have a DataGridView table that is already filled.
Now, when I click on a cell in the DataGridView, I want to make a ComboBox out of the cell, where I can then choose a selection of "Items".
Dgv_Data_List is my DataGridView.
private void Dgv_Data_List_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewComboBoxCell CboCell = new DataGridViewComboBoxCell();
CboCell.Items.AddRange("Yes", "No");
Dgv_Data_List.Rows[e.RowIndex].Cells[e.ColumnIndex] = CboCell; <--At this point my program crashes
}
I don't want any fixed comboboxes. They should be created at runtime as soon as they are needed.
It is unclear how the grid is filled with data. You may get away with this if the columns and rows are manually added, however if the grid has a DataSource, then when you “double-click” into a cell and set the cell as a combo box as you show… then the “current” cells value better be a “Yes” or “No” or you will get the grids DataError complaining about the value in the cell not being a valid combo box value.
I would think you would have to “delete” the current cells value just to avoid the possible error. Something like…
DataGridViewComboBoxCell CboCell = new DataGridViewComboBoxCell();
CboCell.Items.AddRange("Yes", "No");
Dgv_Data_List.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
Dgv_Data_List.Rows[e.RowIndex].Cells[e.ColumnIndex] = CboCell;
Setting the cells value to a blank string will work. HOWEVER, if the column type of the cells underlying data source is of a numeric type… then you will get the grids DataError complaining that the “Yes” or “No” value is not a valid numeric value… so technically this will ONLY work if the column type is a string type column. As most of this is unknown, I can only assume ALL the cells are of a string type.
As the code given, you created comboboxcell but not convert the grid view. so do this instead.
this.dataGridView1[CboCell] = new DataGridViewComboBoxCell();
Try this in the place of creating the comboboxcell.
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.
I have a databound gridview in my winform. I want to know how to get the index of Currently selected rows i.e multiple rows.
I am able to do this with a single row. but is there a way I can have a checkbox or something in which I can index of multiple rows.
The Image below will help u understand better of my requirement.
First set CellContentClick event to your DataGridView.
dataGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.onCellContentClick);
For every cell click it will invoke the following method. Here you can create a list and populate it with clicked row index.
public void onCellContentClick(DataGridViewCellEventArgs cell)
{
// Check whether selected cell is check box column, here 0 indicates the check box column.
if (cell.ColumnIndex == 0)
{
bool isChecked = (Boolean) dataGridView[cell.ColumnIndex, cell.RowIndex].EditedFormattedValue;
if(isChecked)
{
// Below will give you the selected cell row index, for multiple rows you can populate those index in list or whatever you convenient with.
cell.RowIndex;
}
}
}
Use DataGridView.SelectedRows property to get all selected rows.
To select multiple rows:
DataGridView.MultiSelect = true;
I am populating my datagrid based on a value in the textbox.
One of the field values in my grid view is a combobox.On a particular text box entry it shows me the correct results but wen I give another value in the text box, the combobox increases its number which means If I enter 100, the data is populated correctly in my combobox but for any value that is provided next the number of combobox becomes 2.I don't know why this happens. This is the code on button click. please help
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "Supplier";
//execute sql data adapter to get supplier values
DataTable dt = obj.SqlDataTable("select name1 from blahblah");
//foreach (DataRow supplier in dt.DataSet.Tables[0].Rows)
//{
// combo.Items.Add(supplier[0]);
//}
//dataGridView1.Columns.Add(combo);
foreach (DataRow row in dt.Rows)
{
combo.Items.Add(row["NAME1"].ToString());
}
dataGridView1.Columns.Add(combo);
You're adding a new combobox on every click... instead check if the comboboxColumn is already added and appropriately modify its contents if it is already present.
To do this check, name your combobox column like this:
combo.Name = "Supplier";
the name property will not show in the UI, but Windows Forms will remember it. Then, you can test if your column is already added with:
if (!dataGridView1.Columns.Contains("Supplier"))
I'm not sure about the textbox, but everytime you click, a new DataGridViewComboBoxColumn is added to the grid. If you click 5 times, you'll see 5 DataGridViewComboBoxColumns with the dropdown being the result of the sql query.
You would need to check if the DataGridViewComboBoxColumns combo was already added:
if (dataGridView1.Columns.IndexOf(combo) < 0)
{
// Add
}