Keep Getting null on DataRowView' - c#

private void OnDoubleClick(object sender, MouseButtonEventArgs e)
{
DataGrid dataGrid = (DataGrid)sender;
DataRowView row_selected = dataGrid.SelectedItem as DataRowView;
var s = row_selected["Nome"].ToString();
MessageBox.Show(s);
}
I'm trying to make a cell value message box when selected

I don't know what exactly you want to achive but I assume that your trying to pop a message box on double click on selected value on the grid view.
This is done by DataGridView Event.
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
}
Then you can use the DataGridViewCell.Value Property to retrieve the value stored in a particular cell.
MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
So the final code would look like this.
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
}
NOTE change the Gridview name as you datagrid name

Related

C# User selecting a cell in a DataGridView doesn't trigger the SelectionChanged event?

I'm working on a Windows Form that's got two DataGrids.
I'm currently trying to make it so that when one cell is selected in DataGridView1, something else is being displayed in DataGridView2.
The problem is that when I run my app, selecting any of the cells doesn't do anything at all.
I've tried using
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
MessageBox.Show("The selected cell has changed!");
};
to check and see if the event is registering, and nothing happened.
The SelectionMode for the DataGridView is set to CellSelect.
What am I doing wrong?
Thanks In Advance.
Examples:-Check This Also
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 a = Convert.ToString(selectedRow.Cells["you have to mention you cell corresponding column name"].Value);
}
}
Use this And Take reference
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
Another
MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
Check this property Also DataGridView1.FullRowSelect = true

How to get data from the edited cell in datagridview using c# winform?

I have a datagridview who's data source has been set using a list. My datagridview is editable. How can I get the edited cell new value?
I am a beginner in c# winform.
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ Console.WriteLine(Convert.ToString(dataGridView.Rows[e.RowIndex].Cells["id"].Value));
}
Using this code I get the previous value of the cell.
You could use CellValueChanged event.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//do your checks to see RowIndex is not -1 and other good stuffs
var row = dataGridView1.Rows[e.RowIndex];
var changedValue = (string) row.Cells[e.ColumnIndex].Value;
}
You could use the CellEnter event if you want to get the value as soon as the cell is selected event and then to get the value of the selected cell just use:
datagridview1.CurrentCell.Valueif you want a String you can use datagridview1.CurrentCell.Value.toString()
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
var selectedcell = dataGridView1.CurrentCell.Value;
}

c# datagridview in tabpanel

In my form i've a tabpanel with five tab.
Every tab have a datagridview binded to a databes.
All datagrid are filled correctly,but the event CellContentClick works only on the frist tab.
I'm trying to retrive content on selected cell with this code:
//fristtabpag and datagrid1
private void dgw1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
text1.Text = dgw1.SelectedRows[0].Cells[0].Value.ToString();
}
//second tabpag and datagrid1
private void dgw2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
text1.Text = dgw2.SelectedRows[0].Cells[0].Value.ToString();
}
//other....
And it works only on frist tabpage.
i use same code corrrectly fixed for other page, but i get an error for the index.
How fix it?
You could try using the DataGridViewCellEventArgs like this:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
text1.Text = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.SafeToString();
}
Alternatively you could try using the CurrentCellChanged event and get the value from the CurrentCell property:
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv.CurrentCell != null)
{
text1.Text = ((DataGridView)sender).CurrentCell.Value.SafeToString();
}
}
(The method SafeToString() is a simple extensionmethod, all it does is check if the object is null before calling ToString(). If the object is null it returns an empty string)
From your code-example it looks like your eventhandlers does the same thing to different DataGridViews. By using the sender parameter, you can use the same handler for all the DataGridViews.

Problems with DataGridView when add new items

When I save a new item on datagridview the statement
MessageBox.Show(this.tb_aprovacao_admissaoDataGridView.CurrentRow.Cells[0].Value.ToString());
shows the value -1. How can I change it to show the real number of ID?
Thanks to everyone.
Depends how you want to get the value..
Do you want to get the value after you click on the cell, or on the click of a button or?
If you want to do it on the cell click event, you can do it like this:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
To get it with a button:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}

selected item change ComboBox

Is there any way to prevent changing the selected item in a ComboBox only if for certain conditions? I want to allow update the selected item's displayValue in the ComboBox. But I don't want user to change the selected item when it's being updated. This is a windows application.
Inside your class:
private int _selectedIndex = 0;
Inside your form load method:
comboBox1.Enter += new EventHandler(comboBox1_Enter);
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
Then the rest of the code:
protected void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (true) { // Add your validation or certain condition here.
(sender as ComboBox).SelectedIndex = _selectedIndex;
}
}
protected void comboBox1_Enter(object sender, EventArgs e) {
_selectedIndex = (sender as ComboBox).SelectedIndex;
}
Try setting set the Enabled property to false. (Or some third-party toolkits like Telerik have a ComboBox with a ReadOnly property.)

Categories

Resources