Datagrid Get Selected Rows' Column Values

Datagridview full row selection but get single cell value

You can do like this:

private void datagridview1_SelectionChanged(object sender, EventArgs e)
{
if (datagridview1.SelectedCells.Count > 0)
{
int selectedrowindex = datagridview1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = datagridview1.Rows[selectedrowindex];
string cellValue = Convert.ToString(selectedRow.Cells["enter column name"].Value);
}
}

How to get selected row's all column values of dataGridView in TextBoxes

You can change the property definitions to something like this:

public string SomeProperty
{
get
{
string value = null;
if(BindingContext[dataGridView1.DataSource].Current !=null)
{
var r = ((DataRowView)BindingContext[dataGridView1.DataSource].Current).Row;
value = r.Field<string>("SomeDataColumn");
}
return value;
}
}

This way, the SomeProperty will always return the value of SomeDataColumn from the active row.

Get dataGridView selected row value

try this

 richTextBox1.Text=dataGridView1.Rows[e.RowIndex].Cells["Your Coloumn name"].Value.ToString();

DataGrid get selected rows' column values

UPDATED

To get the selected rows try:

IList rows = dg.SelectedItems;

You should then be able to get to the column value from a row item.

OR

DataRowView row = (DataRowView)dg.SelectedItems[0];

Then:

row["ColumnName"];

C# DataGridView get selected Row / Column value

If your SelecetdItem is bound to Property like this:

private DataGridItem selectedDG1;

public DataGridItem SelectedDG1
{
get { return selectedDG1; }
set { selectedDG1 = value;
UpdateProperty("SelectedDG1");
}
}

For DataItem of DataGrid:

public class DataGridItem
{
public string name { get; set; }

public int id { get; set; }
}

Then ID can be get as SelectedDG1.id.

And code System.Data.DataRowView rowview = DG1.SelectedItem as
System.Data.DataRowView;
is wrong. This will only work when
ItemSource is a Datatable. If ItemSource is a collection then:

       var selctedItem = DG1.SelectedItem as DataGridItem;
if (selctedItem != null)
{
int value = selctedItem.id;
}

Anyway you can suppress the exception by putting a Null check over the line: (Also your DataGridColumn is bound to 'id' and you are trying to retrive 'Id' that also can cause null exception)

if (rowview != null)
strid = rowview.Row["Id"].ToString();

How do I get selected row value in C# WPF DataGrid?

Cast the SelectedItem property of the DataGrid to your Item type:

var selectedItem = dgdProcessList.SelectedItem as Item;
if(selectedItem != null)
MessageBox.Show(selectedItem.ID.ToString());

Get the selected values in a datagrid with material ui

If you log your select state you can see that the state is being set according to what is selected. onSelectionChange callback newSelection parameter already contains what you seek.

Sample Image

The main issue with your code is <h1>{select}</h1>. While select is indeed an array and arrays are valid React children, each of your array element contains an object (e.g., firstName, lastName), therefore it won't work with that setup.

Sample Image

You may iterate over the array and print each individual array element object property value.

Example below is printing out firstName:

return (
<div style={{ height: 400, width: "100%" }}>
<h1>{select.map((val) => val.firstName)}</h1>
<DataGrid
rows={rows}
columns={columns}
pageSize={25}
checkboxSelection
hideFooterPagination
onSelectionChange={(newSelection) => {
setSelection(newSelection.rows);
}}
/>
</div>
);


Related Topics



Leave a reply



Submit