Setting default value to a DataGridViewComboBoxCell - c#

The combobox displays a blank field by default even though the combobox is populated with a number of values
ColumnSpeed.DataSource = speedList;
ColumnSpeed.ValueType = typeof(string);
I also tried the following, but it still displays the blank text.
foreach (DataGridViewRow row in myDataGridView.Rows)
{
DataGridViewComboBoxCell cell = row.Cells[ColumnSpeed.Index] as DataGridViewComboBoxCell;
if (cell != null)
{
cell.DataSource = speedList;
cell.Value = cell.Items[0].ToString();
}
}

It could be that the ValueMember you assigned to your DataGridView is different from the DisplayMember you assigned. If that's the case, you'll get a blank value, plus you'll get a DataGridError firing.
You should try:
foreach (DataGridViewRow row in dgMain.Rows){
DataGridViewComboBoxCell pkgBoxCell = row.Cells[ColumnSpeed.Index]
pkgBoxCell.Value = ((Package) pkgBoxCell.Items(0)).Id
}
I converted that from vb.net, so it may not compile. In place of the line where i set the value, do whatever steps are necessary to retrieve and set the correct ValueMember value. In my example, I am casting the item to a specific type and using it's id.

I believe the code you have written should work .. just want to know where are you calling the same. It should work if you call it in the databinding_complete event of the grid

Once you set all the DataSources, try calling the DataGridView.Refresh() method. This is usually required to display changes to the DataSources.

Related

DataGridViewComboBoxCell Value not being set properly

I've been trying to set the value of an individual DataGridViewComboBoxCell for the last 4 hours and I've been getting nowhere. The most common solution I've seen was to set the .Value member of the DataGridViewComboBoxCell to one of the Items, which I tried and it complained the value was not valid.
DataTable documentTypes = _codedValues.GetCodedValues(Database.DOCUMENT_TYPE_TABLE); documentTypes.Columns[Database.PROFESSION_ID_COLUMN].AllowDBNull = true;
documentTypes.Columns[Database.CODE].AllowDBNull = true;
this.cbxDocumentType.DisplayMember = Database.VALUE;
this.cbxDocumentType.ValueMember = Database.CODE;
this.cbxDocumentType.DataSource = documentTypes.DefaultView;
int rowId = this.dgvDocumentList.Rows.Add(doc.actualName, doc.fileName);
DataGridViewComboBoxCell obj = (DataGridViewComboBoxCell)this.dgvDocumentList.Rows[rowId].Cells[2];
obj.Value = obj.Items[0];
After the message box comes up that tells me DataGridViewComboBoxCell view is not valid, I see the .ToString output of the object being set, which is System.Data.DataRowValue.
Depending what your datasource contains you must use the correct casts to access the correct fields.
Try this:
DataGridViewComboBoxCell cell =
(DataGridViewComboBoxCell)this.dgvDocumentList.Rows[rowId].Cells[2];
cell.Value = ((DataRowView)cell.Items[0]).Row.ItemArray[0];
This assumes that the Items are DataRowViews and that the ValueMember is in the first field.
You can test the type by writing an intermediate step:
var item = cell.Items[0];
And use the debugger to look into the resulting type..

Is there a way to make a cell editable where column is readonly in XtraGrid?

My grid looks like this.
Key Value
1 A
2 B
3 C
I have Value column read-only in my grid. "Value" column's columnedit is associated with memoexedit repository. Now what I want to do is on some condition like when key=2,
I want my value cell to be editable.
I tried making the whole column ReadOnly = false.
and then handled ShowingEditor to cancel edit on everything else than Key=2, but that prevents even opening up the editor for other values.
What I want is able to see the editor but it should be readonly for Others
and for Key=2, It should be editable.
Please help!
Try to handle the ShownEditor event as the following (semi-pseudo code):
var grid = sender as GridView;
if (grid.FocusedColumn.FieldName == "Value") {
var row = grid.GetRow(grid.FocusedRowHandle) as // your model;
// note that previous line should be different in case of for example a DataTable datasource
grid.ActiveEditor.Properties.ReadOnly = // your condition based on the current row object
}
This way you could refine the already opened editor with your needs.

Data-Bound Combobox sometimes returning Value other than the one selected as displaymember

This is all the code related to the combox.
string SelectedDealer = Dealers.SelectedValue.ToString();
this.Dealers.DataSource = this.dealersBindingSource;
this.Dealers.DisplayMember = "DealerName";
this.Dealers.ValueMember = "DealerName";
The Dealers table has only two columns. What happens is, sometimes(one or two times/ application run) the
Dealers.SelectedValue.ToString();
returns the value of the other data-member,letsay Dealer-Id; right,that is Pretty weird.
Any help would be appreciated.
Try changing order of lines, who knows, it may help :) Maybe Combobox sets something default to ValueMember after setting its datasource if ValueMember is not previously set.
this.Dealers.DisplayMember = "DealerName";
this.Dealers.ValueMember = "DealerName";
this.Dealers.DataSource = this.dealersBindingSource;
string SelectedDealer = Dealers.SelectedValue.ToString();
Also check if something is in fact selected in your combobox: SelectedIndex of combobox souldn't be -1 (SelectedValue then may be unspecified, although it should throw NullPointerException)

How do I get the selected row data from a data grid view using SelectedRows?

I have a table that I am displaying in a data grid view control. The user selects a single row from the control and presses a button. I need to retrieve the cells from that row and store them as strings.
Exactly how do I get the data using the SelectedRow method? I've been working on this for several hours and I'm at the end of my rope. Here's an example of something I've tried:
DataGridViewCellCollection selRowData = dataGridView1.SelectedRows[0].Cells;
If I try to access selRowData[x], the return value does not contain my data.
You're close - you need to reference each Cell through its index and return its Value property:
string firstCellValue = dataGridView1.SelectedRows[0].Cells[0].Value;
string secondCellValue = dataGridView1.SelectedRows[0].Cells[1].Value;
etc.
If you want the data and the data is likely bound to an datasource, then might I suggest that you get the key from the selection, and then you can use that to access the data any way you like:
dataGridView.SelectedDataKey.Value;
Try using the Item element of the dgv.
dgvFoo.Item(0, dgvFoo.CurrentRow.Index).Value
That would return the value of the first item. You could put that into a for loop to get them all.
Another option would be to use the SelectedRows collection on the object and iterate through each selected row (or just the one in your case).
Well there is no datagridview Item property..#Jay Riggs solution is better...Following solution also works:
string firstCellValue = dataGridView1[0,dataGridView1.CurrentRow.Index].Value.ToString();
string secondCellValue = dataGridView1[0,dataGridView1.CurrentRow.Index].Value.ToString();
Here 0 is the first column and dataGridView1.CurrentRow.Index is the current Row from where to get value.
Perhaps this is a more suitable solution use the cell values of the row clicked:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
var val = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
}

Can't get values from rows/cells in GridView

I'm trying get values from a GridView using the following code:
foreach (GridViewRow row in this.dgvEstudios.Rows)
{
var xy = row.Cells[1].Text;
}
Always get a an empty string ("") as the value returned from .Text, why does this happen? I have set EnableViewState to true
If there are controls in each cell you will need to get the value like this:
Label lblEmailAddress = GridView.Rows[e.CommandArgument].FindControl("lblEmailAddress");
string Email = lblEmailAddress.Text;
in that case it it the control in the cell that has the value not the cell iteslf.
The cell might have controls inside it (e.g. LiteralControl or an HyperLink). This is what you should be looking for.
row.Cells[1].Controls collection, you should look for.
it could depend on many things.. Where is this code fired in relation to when the GridView is populated (Databind() called)?
Without any context, its hard to say what else it could be.

Categories