My datagridview is in RowHeaderSelect mode. So clicking on the RowHeader selects the whole row.
However, at any point, when i use context menu shortcuts or shortcut keys from the keyboard, i need to check if a whole row is currently selected, or just a single cell, and perform actions accordingly. How do I check this?
you can check with e.CommandName property.
Check foolowing patch of code>>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
SXEngine.Classx USER = (SXEngine.Classx)Session["APPOBJ"];
if (e.CommandName == "Select")
{
USER.bRowSelect = true;
}
else
{
USER.bRowSelect = false ;
}
}
Study this link to get more info regarding different properties of gridview>>
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanging.aspx
Use CommandName method of RowCommand event's argument e.
like
if(e.CommandName=="Select")
{
//code
}
Related
Pressing Tab key on Button Refresh is setting focus on the dropdown list but I need to set focus on Checkbox column and first row of grid when the grid datasource is not null else the next control, however it is selecting the given cell only. I have set tabIndex property in sequence, please tell me where i am wrong, here is my code:
private void btnRefresh_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
if (grid.DataSource != null)
{
grid.Focus();
grid.CurrentCell = this.grid[1, 0];
grid.CurrentCell.Selected = true;
grid.BeginEdit(false);
}
else
{
btnCancel.Focus();
}
}
}
Have you seen this post?
Seems like your use of the index is of Grid[x,y].
Try
grid.Rows[1].Cells[0]
However, this wil select only the cell (first cell, second row by the way).
If you want to select the entire row, try
grid.Rows.First().Selected = True
Hope it helps.
I need to find a RowIndex of the current row by selecting anywhere on the Gridview without SelectCommand. Basically i need it because i am creating a method which will return the Object depending on the DataKey of the SelectedRow. And i am calling it everywhere on the Page and i do not want to write the same code again an again.
Here is what i have.
On RowDataBound
protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dataKey = gvOrders.DataKeys[e.Row.RowIndex];
if (dataKey == null)
return;
int orderId = AlwaysConvert.ToInt(dataKey["OrderId"]);
Order cncOrder = OrderDataSource.Load(orderId);
// do some work
}
}
Now i have a 3 CheckBox column in gridview, so whenever i check the CheckBox state i am doing the following and load the Object again and do some database work.
On CheckBox Change Event
protected void cbIsReceived_CheckedChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
var dataKey = gvOrders.DataKeys[row.RowIndex];
if (dataKey == null)
return null;
int orderId = AlwaysConvert.ToInt(dataKey["OrderId"]);
Order cncOrder = OrderDataSource.Load(orderId);
// find the controls using the current row index
CheckBox cbIsReceived = (CheckBox)gvOrders.Rows[row.RowIndex].FindControl("cbIsReceived");
Label receivedDateText = (Label)gvOrders.Rows[row.RowIndex].FindControl("receivedDateText");
// do some work
}
As you can see in both events most of the code is to find the current RowIndex and load the Object and then do some database work.
if you notice, in both event the arguments supplied is different i.e OnRowBound its (GridViewRowEventArgs e) and OnCheckBox_CheckedChanged its (EventArgs e).
So for every CheckBox column i have to write the same code again and again.
I would like to create method where i can pass the Sender and get the current RowIndex back. I am not sure how to do that. please help.
You can route the events from all 3 CheckBoxes to the same method by either:
Just supply cbIsReceived_CheckedChanged as the event handler for
all three checkboxes OR:
If you've already generated methods to handle the events for all
the other CheckBoxes, then do this for all the auto-generated methods:
cbAnotherCheckBox_CheckedChanged(Object sender, EventArgs e)
{
// call the first method you've already written:
cbIsReceived_CheckedChanged(sender, e);
}
Edit:
If there are different operations for each of the CheckBoxes, then loop through the rows to get the particular CheckBox that was checked and then run the operation that's specific to it.
In winforms, you need to click the combobox twice to properly activate it - the first time to focus it, the second time to actually get the dropdown list.
How do I change this behavior so that it activates on the very first click?
This is for DATAGRIDVIEW combobox.
I realize this is an old question, but I figured I would give my solution to anyone out there that may need to be able to do this.
While I couldn't find any answers to do exactly this... I did find an answer to a different question that helped me.
This is my solution:
private void datagridview_CellEnter(object sender, DataGridViewCellEventArgs e)
{
bool validClick = (e.RowIndex != -1 && e.ColumnIndex != -1); //Make sure the clicked row/column is valid.
var datagridview = sender as DataGridView;
// Check to make sure the cell clicked is the cell containing the combobox
if(datagridview.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && validClick)
{
datagridview.BeginEdit(true);
((ComboBox)datagridview.EditingControl).DroppedDown = true;
}
}
private void datagridview_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
datagridview.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
The above code must be tied into the CellEnter event of the datagridview.
I hope this helps!
edit: Added a column index check to prevent crashing when the entire row is selected.
Thanks, Up All Night for the above edit
edit2: Code is now to be tied to the CellEnter rather than the CellClick event.
Thanks, HaraldDutch for the above edit
edit3: Any changes will committed immediately, this will save you from clicking in another cell in order to update the current combobox cell.
Set the following on your DataGridView:
EditMode = EditOnEnter
This is probably the easiest solution and has been the workaround for many users here on SO when this question gets asked.
EDIT :
Per here do the following:
Set the Editmode:
EditMode = EditOnKeystrokeOrF2
Modify the EditingControlShowing event on the datagridview:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox ctl = e.Control as ComboBox;
ctl.Enter -= new EventHandler(ctl_Enter);
ctl.Enter += new EventHandler(ctl_Enter);
}
void ctl_Enter(object sender, EventArgs e)
{
(sender as ComboBox).DroppedDown = true;
}
This will get you your desired results. Let me know if that doesn't do it.
I changed only the EditMode property of the datagridview to EditOnEnter and it's working perfectly.
EditMode = EditOnEnter
If you set the entire grid to EditOnEnter, you can get some pretty funky activity when you are on a text column. Here's my solution, which should be self explanatory. If you did not know the column names, you could just check the cell type on mousemove.
Private Sub GridView_CellMouseMove(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles GridView.CellMouseMove
Select Case GridView.Columns(e.ColumnIndex).Name
Case "Ad_Edit", "Size_Caption", "Demo_Code"
GridView.EditMode = DataGridViewEditMode.EditOnEnter
Case Else
GridView.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
End Select
End Sub
Set the DropDownStyle property of your combo box to DropDownList...
Perhaps old.. But make sure to set ReadOnly property to false, else the cell wont enter editmode and therefore the EditingControl returns null and casting DroppedDown = true will cast a NullReferencException.
i'm trying to set a cell to edit mode. The cell is in the new row (NewRowIndex). Everwhere else it works well, but if i try to set the edit mode in the NewRowIndex, it doesn't get into edit mode as supposed. I simply want that i f user enters a new row (NewRowIndex), the first cell get into edit mode. I've tried (on RowEnter):
dgvList.CurrentCell = dgvList["myColName", dgvList.NewRowIndex]; dgvList.BeginEdit(true);
Thank you!
I dont think you really need to utilize the NewRowIndex property. Just begin edit by setting the current cell:
private void dgvList_CellEnter(object sender, DataGridViewCellEventArgs e)
{
dgvList.CurrentCell = dgvList[e.ColumnIndex, e.RowIndex];
dgvList.BeginEdit(true);
}
If you want the cell to go in edit mode only for new rows, then:
private void dgvList_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != dgvList.NewRowIndex)
return;
dgvList.CurrentCell = dgvList[e.ColumnIndex, e.RowIndex];
dgvList.BeginEdit(true);
}
Edit: If you want the new row to start being in edit mode upon keydown, then that's a feature already available for datagridviews. You can manually set it like this:
dgvList.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
//or
dgvList.EditMode = DataGridViewEditMode.EditOnKeystroke;
If you want the cell to be in edit mode upon keydown only for new rows, then you will have to override the default behaviour, by hooking KeyDown event, which I believe is a bad way f doing GUI. May be like this:
Initialize: dgvList.EditMode = DataGridViewEditMode.EditOnF2; //or whatever you prefer
to override the default excel style editing upon keystroke. And then
private void dgvList_KeyDown(object sender, KeyEventArgs e)
{
if (dgvList.CurrentCell.RowIndex != dgvList.NewRowIndex)
return;
dgvList.BeginEdit(true);
}
Don't ask why, but I need a way to prevent the user from entering a cell in the 'new' datagridview row WHILE they've got multiple rows selected.
Currently, the cell in the first column of the row that the mouse is hovering over during the click and drag is being selected. If you click on the cell, then the rows aren't selected anymore, so you can't use any cell click events or anything.
Any suggestions are welcome.
P.S. don't try editing the currentcell from the selectionchanged event, already tried that!
Thanks,
Isaac
I have an idea that the DataGridView control has a RowState property. When your row is selected, check the state. I'm not sure what the states are (if I'm even in the ballpark) ... anyway, you may be able to check that route.
I'm looking for more info ...
Okay ... think I found a decent way to do this:
void DataGridView1_RowsAdded (object sender, DataGridViewRowsAddedEventArgs e)
{
currentNewRow = e.RowIndex;
}
void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == currentNewRow)
{
// don't add to Selection
}
}
Use CellStateChanged event:
private void dataGridView_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e) {
if (e.Cell.OwningRow.IsNewRow && dataGridView.SelectedRows.Count > 1)
e.Cell.Selected = false;
}