Few questions about DataGrid...

G

Guest

hello
i have a DataGrid Table and i want to select a row by Clicking the mouse
after i tried few times, i understood the the only event the the datagird respones to is the current_cell_chang
and my code for this event was
private void dataGrid1_CurrentCellChanged(object sender,System.EventArgs ne

dataGrid1.Select(dataGrid1.CurrentCell.RowNumber)
this.dataGrid1.Focus()

the problem is that it's selects the whole row except the current_cell (it sets the focus on that cell) and the problem is that i can't make the dataGrid to respone to other events..

what should be the problem? or what is the solution

thanks
 
A

adnan boz

Hi,

The sample below will help you further.

Good Luck
Adnan

Sampel use of the MouseUp event
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
DataGrid myGrid = (DataGrid)sender;
DataGrid.HitTestInfo myHitInfo = myGrid.HitTest(e.X, e.Y);
if (myHitInfo.Type == DataGrid.HitTestType.Cell || myHitInfo.Type == DataGrid.HitTestType.RowHeader )
{
myGrid.Select(myHitInfo.Row);
myGrid.CurrentRowIndex = myHitInfo.Row;
int someSampleID = Convert.ToInt16(myGrid[myHitInfo.Row,0]);
}
}


hello,
i have a DataGrid Table and i want to select a row by Clicking the mouse.
after i tried few times, i understood the the only event the the datagird respones to is the current_cell_change
and my code for this event was:
private void dataGrid1_CurrentCellChanged(object sender,System.EventArgs ne)
{
dataGrid1.Select(dataGrid1.CurrentCell.RowNumber);
this.dataGrid1.Focus();
}
the problem is that it's selects the whole row except the current_cell (it sets the focus on that cell) and the problem is that i can't make the dataGrid to respone to other events...

what should be the problem? or what is the solution?

thanks
 
M

Morten Wennevik

Hi Gidi,

I'm a little confused. The current cell is selected with the rest of the row, and clicking on a cell causes it to change to edit mode. If you don't want the user to edit the values, why not simply use a ListView instead, with FullRowSelect = true

You might want to take a look at DataView, which has more functionality than a DataGrid.

Happy coding!
Morten Wennevik [C# MVP]
 
G

Guest

Morten,
thank you it worked...
now i have another question...
if i'm pressing the scroll arrows (move the datagrid left\right\up\down) the cell which i pressed gets the focus (as before)
how can i make the row stay selected even if i pressed the scroll arrow
 
M

Morten Wennevik

That is a very good question, that I don't know the answer for. You need to trap the key events first, but even though a datagrid has key events they never seem to fire. Setting the parent form's KeyPreview won't give it key events from the datagrid either. Nor does inheriting DataGrid and overriding the OnKeyDown event. Bug?

Happy coding!
Morten Wennevik [C# MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top