C# Convert DatagridViewTextbox to DataGridViewComboBoxCell - c#

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.

Related

How can I get DataGridView combobox cell value to be written in the MessageBox in C#?

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

Set default value for DataGridView ComboBox

My application consist of DataGridviewComboBoxColumn inside DataGridView. The ComboBoxColumns are getting filled from a database table (key, value pair). I am trying to set the default value for ComboBox column using DefaultValuesNeeded event but it is not working.
Following is the sample code:
e.Row.Cells["Job"] as DataGridViewComboBoxColumn).Value ="12"
But it shows 12 as value , instead of 12 it suppose to show actual text of 12 value.
For example:
DataGridViewComboBoxColumn dgvCbJob = new DataGridViewComboBoxColumn();
{
dgvCbJob.HeaderText = "Job";
hadd.Clear();
hadd.Add("#Search", string.Empty);
ds = ObjDAL.GetDataSetForPrc("prc_GetJobList", hadd);
if (ds.Tables[0].Rows.Count > 0)
{
dgvCbJob.DataSource = ds.Tables[0];
dgvCbJob.DisplayMember = "JobName";
dgvCbJob.ValueMember = "JobMasterId";
}
dgvCbJob.DisplayIndex = 0;
dgvCbJob.Width = 100;
dgvCbJob.Name = "Job";
}
To set default value for cell you can use either of these options:
Handle DefaultValuesNeeded event of grid and assign value to e.Row.Cells["Job"].Value
In your DataTable set the DefaultValue for the 'Job' DataColumn to desired value
In both options, the type of value which you assign should be the same type as DataType of the column.
Note: You should know e.Row.Cells["Job"] is not DataGridViewComboBoxColumn. If the column is combo box, then the cell is DataGridViewComboBoxCell.
In many instances this can be done in design mode, right click dgv, select the combobox column, default cell style, Data "nullValue"
Great for simpler situations like a simple choice A B C for the user, to default to A, if you later compare with the cell's value being A or B or C, it will work. It would be a problem if you compare with the selected index however, as it counts as a value out of the collection i believe

Show row numbers in GridControl PrintPreview

I have problem with DevExpress GridControl PrintPreview. How can I create column with row numbers in PrintPreview? I need to show ordinal number for each row PrintPreview.
Thanks for help.
I think the easiest way to do this from a direct export is to add an unbound data column:
Go into the grid designer and add a column; name it (colRowNumber or whatever)
Move the column to be the first column in the layout
In the column's properties:
a. set UnboundType to Integer
b. set OptionsColumn.AllowEdit set it to False
Within the grid, create an event for CustomUnboundColumnData
Your code for the CustomUnboundColumnData event should look something like this:
private void gridView1_CustomUnboundColumnData(object sender,
DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column == colRowNumber)
e.Value = e.ListSourceRowIndex + 1;
}
From here, now matter how your grid is ordered or filtered, the "Row Number" column will always contain the 1-based row number of the displayed data.

Set Combo Box in Datagridview Column

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

How do I select a value in a DataGridViewComboBoxCell?

I have DataGridViewComboBoxCell and a DataTable. The data in Table I bound with DataGridViewComboBoxCell using DataSource and set ValueMember, and DisplayMember.
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
dataGridView1.Rows[0].Cells[0] = comboBoxCell;
comboBoxCell.DataSource = dataTable;
comboBoxCell.ValueMember = "ID";
comboBoxCell.DisplayMember = "Item";
}
How can I programmatically set the value in the cell when the form loads?
In the simple ComboBox I know a property SelectedIndex.
I tried comboBoxCell.Value = ...; but it gives an exception.
And tried
private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
e.Value = 1;
}
It sets a new value in the cell, but I need to select a value.
Form loaded and I have empty cell.
And some data in the ComboBox.
When I put this code dataGridView1.Rows[0].Cells["ComboColumn"].Value = "1"; right after comboBoxCell.DisplayMember = ... (see above), it works fine.
The value "1" in the ID column corresponds to the value "Second" in the Items column.So, I get the correct result.
Sorry for my English and my newbie code :)
Instead of adding a cell to your grid add a DataGridViewComboBox column.
DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
c.Name = "ComboColumn";
c.DataSource = dataTable;
c.ValueMember = "ID";
c.DisplayMember = "Item";
dataGridView1.Columns.Add(c);
To select a particular value you set the Value property of a given cell.
dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = 1;
Note that the type here is important! In comments you say you get a System.FormatException. This can be caused by setting the wrong type to the value.
When you set the value to 1 you are assigning an int - if for some reason you have strings in the ID column you will get the System.FormatException exception you are seeing.
If the types differ you need to either update the DataTable definition or set the value to a string:
dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1";
Also note that this value must be present in the ID column of the DataTable that you have set as the source of the grid.
It is usually easiest to work with a DataGridView when it has its DataSource set. In this case you can bind the ComboBoxColumn to the grid's DataSource using the DataPropertyName property.
c.DataPropertyName = "GridDataSourceColumnName";
This allows the columns value to be taken from the grid data source and for changes to the column to directly change that data source.
Lastly, do not use the CellFormatting event here - this event is not intended for this sort of use. It is usually best to do this sort of work in the DataBindingComplete event (if you only want it done once) or during some event like DefaultValues needed or RowValidating.
By using CellFormatting you will probably make it impossible for users to manually edit the combo box.

Categories