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.
Related
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
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
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;
}
I want to create DataGridViewComboBoxColumn and attach to it's combo box Click event (i want to generate it's datasource on click only).
While I have no idea about why you need Click event of that ComboBox control,
you can access that combo box using EditingControlShowing event:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//Check if the event is for your column, for example column 1
if (this.dataGridView1.CurrentCell.ColumnIndex == 1)
{
var comboBox = e.Control as DataGridViewComboBoxEditingControl;
comboBox.Click -= comboBox_Click;
comboBox.Click += comboBox_Click;
}
}
private void comboBox_Click(object sender, EventArgs e)
{
var comboBox = sender as DataGridViewComboBoxEditingControl;
//use comboBox here
}
But you should know, you can set the DataSource for you column in CellClick event of your datagridview too:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex==1 && e.RowIndex>-1)
{
//Check if the event is for your column, for example column 1
var column = (DataGridViewComboBoxColumn)this.dataGridView1.Columns[e.ColumnIndex];
//use column.DataSource
}
}
And another important thing you should know is if you set a datasource that not contains the value of one of cells of this column, you will receive errors while rendering the column.
In the first column of my datagridview, I have checkboxes and I want to fire an event each time the status of the checkbox is changed. I thought of using the cellcontentclick event, casting the sender object to datagridviewcell and checking by its column index. But I found out that the sender object is a datagridview object. So, how to perform the desired operation?
To handle CheckBoxCell value changed you have to use this event CellValueChanged. Sender in events will always be the control that raised the event. To have more information about what happend you need to take a look into EventArgs.
Back to handling CheckBoxCell do this:
private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
var dgv = sender as DataGridView;
var check = dgv[e.ColumnIndex, e.RowIndex].Value as bool?;
if (check.HasValue)
{
if (check)
{
//checked
}
else
{
//unchecked
}
}
}
Hope this helps :)
There are many methods
One Method is :
You can take a hidden field or viewstate on page in which you can store row id when click occured by javascript and then in code behind get that hiddenfield value.
Other one :
You can use CommandName & CommandArgument and in code behind use datagridview_ItemCommand
private void dgvStandingOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvStandingOrder.Columns[e.ColumnIndex].Name == "IsSelected" && dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
{
bool isChecked = (bool)dgvStandingOrder[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
if (isChecked == false)
{
dgvStandingOrder.Rows[e.RowIndex].Cells["Status"].Value = "";
}
dgvStandingOrder.EndEdit();
}
}
private void dgvStandingOrder_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dgvStandingOrder_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
{
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}