I have DataGridView to load some data programmatically. After inserting my data I am showing the DataGridView . Here by default the 1st row 0th column cell is selected. But I don't need that. I have tried to disable that option.
datagridviewname.currentcell=null
But it will not work. Any body can help me to solve my problem.
Set CurrentCell Selected property to False like:
dataGridViewName.CurrentCell.Selected = false;
On the DataGridView create an event for DataBindingComplete then add this method datagridview1.ClearSelection()
private void datagridview1_DataBindingComplete(object sender, EventArgs e)
{
datagridview1.ClearSelection();
}
Why u set it null? It should be like following. I think it will work
dataGridViewName.Rows[0].Cells[0].Selected = false;
if it is 1st row 0th, then
dataGridViewName.Rows[1].Cells[0].Selected = false;
The only way is this:
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
dataGridView.ClearSelection();
}
You should perform clear selection inside form load event of the form instead of constructor.
private void Form1_Load(object sender, EventArgs e)
{
// You will get selectedCells count 1 here
DataGridViewSelectedCellCollection selectedCells = dataGridView.SelectedCells;
// Call clearSelection
dataGridView.ClearSelection();
// Now You will get selectedCells count 0 here
selectedCells = dataGridViewSchedule.SelectedCells;
}
MSDN
You can set this property to null to temporarily remove the focus rectangle, but when the control receives focus and the value of this property is null, it is automatically set to the value of the FirstDisplayedCell property.
So looks like setting it to null only works if it wasn't the first row first column cell.
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 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
}
I have a datagridview. It has 2 columns which are of type boolean. Now what I want to do is, when I click on one checkbox the other check box in the same row should be unchecked. that is, at a time only one check box should be selected in a particular row. The user should not be able to select the 2 check boxes together.
How can I achieve this ? I tried using cellcontentclick and cellcontentChanged events. nothing works.
any inputs ??
You can use CellEndEdit Event or CellLeave event
when ever you will leave the cell after doing edits this event will fire.
In the following snippet Column1 is your first checkbox and Column2 is your other checkbox.
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value))
dataGridView1.Rows[e.RowIndex].Cells["Column2"].Value = false;
}
Use DataGrid_CellEditEnding
In that iterate through DataSource, if u find any checked colum (boolean true) make it unchecked (boolean false).
hope it will help u
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
foreach (var Item in dataGridView1.ItemsSource)
{
if(Item.isChecked)
Item.isChecked = false;
}
if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value))
dataGridView1.Rows[e.RowIndex].Cells["Column2"].Value = false;
}
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
How can i check for the bool condition of a check box which is in datagridview. I would like to have true if checked and false if it was unchecked. Can any one help me.
Is it possible to handle this in dataGridView_CellContentClick
This is addressed a little bit on the MSDN pages for the DataGridView here and here.
In particular they say:
For clicks in a DataGridViewCheckBoxCell, this event occurs before the
check box changes value, so if you do not want to calculate the
expected value based on the current value, you will typically handle
the DataGridView.CellValueChanged event instead. Because that event
occurs only when the user-specified value is committed, which
typically occurs when focus leaves the cell, you must also handle the
DataGridView.CurrentCellDirtyStateChanged event. In that handler, if
the current cell is a check box cell, call the DataGridView.CommitEdit
method and pass in the Commit value.
So they recommend against using the CellClick type events (since they never push the value until you leave the cell) but instead use CurrentCellDirtyStateChanged and the CommitEdit method.
So you end up with:
dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "CB")
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
And as for getting the checked value - this is just the Value property of the DataGridViewCheckBoxCell.
So if you go:
dataGridView1.Rows[rowindex].Cells[cellindex].Value
you get a boolean value which corresponds to the checkbox (after the change has been committed).
if the checkbox is defined in the designer it would be as simple asreferring to the checkbox´s name and checking its "checked" property for true/false.
But i suspect you are adding the checkbox to the datagrid by code?
in this case youll have to save a reference to the checkbox somwhere.
If i where you i would add all the checkboxes that i add to the datagrid to a list or if you want to refer to them by name i would add them to a dictionary.
You could also bind an event to the checkbox Checked_Changed event by selecting it and clicking the little bolt icon in the properties panel and finding the checkedChanged-event and doubleclicking it.
in the event-code you can obtain the checkbox clicked by typing:
CheckBox mycheckbox = sender as CheckBox;
and then refering to mycheckbox.checked to get a bool for if its checked or not.
You can try to get this in this fashion, say in case you are looping the grid the based on the index you can find the checked state.
bool IsChecked = Convert.ToBoolean((dataGridView1[ColumnIndex, RowIndex] as DataGridViewCheckBoxCell).FormattedValue))
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var checkcell = new DataGridViewCheckBoxCell();
checkcell.FalseValue = false;
checkcell.TrueValue = true;
checkcell.Value = false;
dataGridView1[0, 0] = checkcell; //Adding the checkbox
if (((bool)((DataGridViewCheckBoxCell)dataGridView1[0, 0]).Value) == true)
{
//Stuff to do if the checkbox is checked
}
}
It works 100 %.
private void grd_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
grd.CommitEdit(DataGridViewDataErrorContexts.Commit);
bool Result = Convert.ToBoolean((grd[e.ColumnIndex, e.RowIndex] as DataGridViewCheckBoxCell).Value);
}