I am looking to retrieve the values of a datagrid which is bound to a sqlexecute query.
defects.DefectsDataGrid.DataContext = searchQuery.ExecuteReader();
Then, I am using SelectedCellsChanged event to do something with the DataGridRow selected.
When I put a breakpoint in, I can see the values beneath System.Data.Common.DataRecordInternal > Non-public Members > _Values. But I am not sure how to reference the _Values.
Thanks for your help as always.
Code
private void DefectsDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
//Retrieve selected row
var rows = DefectsDataGrid.SelectedItems;
//This tells me I have one row, which is of type System.Data.Common.DataRecordInternal
//How do I retrieve the values from this type?
}
Looks like you need to use a DataReader to cast your records to IDataRecord.
See this answer : https://stackoverflow.com/a/6034408/414314
defects.DefectsDataGrid.DataContext = searchQuery.ExecuteReader();
I changed the above to the following, which gave me a data type of DataRowView instead of DataRecordInternal
defects.DefectsDataGrid.ItemsSource = DataTable.DefaultView;
This allowed me to easily reference the row in the event.
private void DefectsDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
//Retrieve selected value from the row selected
DataRowView dataRow = (DataRowView)DefectsDataGrid.SelectedItem;
string phrase = dataRow[2];
}
Related
I want to store on a variable the value of the first column of a row whenever I click on a cell.
You can try this code :
private void MyDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
var val = MyDataGrid.Rows[e.RowIndex].Cells[0].Value;
}
Hope this help you.
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;}
I have gridcontrol that has a RepositoryLookupEdit in one of the columns. I can get the value of RepositoryLookupEdit after changed, but I dont know how to get the which row's RepositoryLookupEdit value changed. How can I get the Row ID?
With the code below, I can get the RepositoryLookupEdit value.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
}
Since repositoryItemLookUpEdit isn't restricted to GridControls you cannot get the row handle from this event. You however have other possibilities.
First, if the edit is done by the user, you can use the ColumnView.GetFocusedRow() method to get the current grid row.
If however the edit value is changed via code it will also be changed in the grid so you can now use the ColumnView.CellValueChanged event.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
gridRow = gridView.GetFocusedRow() as MyDataRow
}
What I want to do is to change value of row's forth cell if cell number 3 is changed. I have an EditEnding method for my grid. That's my method below. I don't really know how to finish it
that's the grid definition:
<DataGrid x:Name="dataGrid1"... CellEditEnding="dataGrid1_EditEnding">
and the method:
private void dataGrid1_EditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
// initializing DataRowView from my datagrid
DataRowView drv = (DataRowView)dataGrid1.CurrentItem;
// checking if there were any changes
if (drv.Row[3, DataRowVersion.Original] != drv.Row[3])
{
//set value to cell
}
}
Well, i did my stuff, just forget to post it here.
First I did it with EditEnding event, it looked like that:
private void dataGrid1_EditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataRowView drv = (DataRowView)dataGrid1.CurrentItem;
if (drv.Row[3, DataRowVersion.Original] != drv.Row[3])
{
rowView.Row.SetField(4, /* my logic here */);
}
}
The problem was it was adding the value only on second edit. Then I changed my idea and added a RowChanged event to my DataTable, which was like that:
static void dtSP_RowChanged(object sender, DataRowChangeEventArgs e)
{
bool temp = false;
try
{
temp = e.Row[4, DataRowVersion.Original] == e.Row[4];
}
catch { }
if (temp && int.Parse(e.Row[3].ToString()) != -1)
{
e.Row[4] = (/* my logic */);
}
}
The method was going into infinity loop (it was noticing, that fourth row had changed).
And then i saw this:
http://www.windowsdevcenter.com/pub/a/dotnet/2003/05/26/datacolumn_expressions.html
I've ended with one line long code:
dtSP.Columns[4].Expression = "expression";
#blindmeis, I forgott to mention I use ADO.NET, sorry
do not edit the datagridrow - edit the underlying object in wpf!
this mean when the bound property to cell 3 is changed then do your changes to the property bound to cell 4. INotifyPropertyChanged will notify your grid and will show your changes
If you already have logic to calculate cell4 value when cell3 is changed, then when property binded to column 3 is changed you should call INotifyPropertyChanged of property binded to column 3 & 4.
Suppose if I say I have selected a row in gridview and want that row to come up in to their respective textboxes. For example, I have selected a row which has First Name and Last Names and on selection of row, the data from gridview should come in to textbox on winform. Please tell me.
I am guess it is something based on selection changed event if I am right ? Like below:
private void dgv_SelectionChanged(object sender, EventArgs e)
{
string str = txtFirstName.Text;
DataGridViewRow selectedRow;
}
If I got your question right, your solution is to use DataGridView.SelectedRows Property along with DataGridView.SelectionChanged Event
DataGridView.SelectedRows Gets the collection of rows selected by the
user.
DataGridView.SelectionChanged occurs whenever cells are selected or
the selection is canceled, whether programmatically or by user action.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows) {
//Send the first cell value into textbox'
Txt_FirstName.Text = row.Cells(0).Value.ToString; // or row.Cells["ColumnName"].Value;
}
}
}
MSDN and MSDN