Hi,
I have a datagrid bound to a dadaset table. I programatically added a new row. I wonder what is the easiest way to make the current cell or row to the newly added row in the datagrid?
Thanks.
Dion
Printable View
Hi,
I have a datagrid bound to a dadaset table. I programatically added a new row. I wonder what is the easiest way to make the current cell or row to the newly added row in the datagrid?
Thanks.
Dion
datagrid's Select
property
Thanks for your help.
How do I find the newly added row number though?
Dion
just ,
int nRow = dataGridDetails.BindingContext[dataGridDetails.DataSource, dataGridDetails.DataMember].Count;
Paresh
Thanks again.
This only gives me the total row count, but not the newly added row index ( row number ), so that I can select though.
Also, datagrid.select( int) selects the row, but the arrow on the Row Header did not move to the selected row. Any ideas?
Dion
1) you could maintain the count. when you add/delete rows you maintain the count by doing n++ or n--
2) or you could do by
DataTable myTable = new DataTable("Persons");
...
DataColumn cPerson = new DataColumn("PersonID", typeof(int));
cPerson.AutoIncrement = true;
cPerson.AutoIncrementSeed = 1;
cPerson.AutoIncrementStep = 1;
myTable.Columns.Add(cPerson);
3) if you hit the mouseclick on a cell you can select the row by
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if(hti.Type == DataGrid.HitTestType.Cell)
{
dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
dataGrid1.Select(hti.Row);
}
}
the following is the method for when you click on a row header the row is selected.
public class MyDataGrid : DataGrid
{
public const int WM_LBUTTONDOWN = 513; // 0x0201
public const int WM_LBUTTONUP = 514; // 0x0202
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);
public void ClickRowHeader(int row)
{
//get a point to click
Rectangle rect = this.GetCellBounds(row, 0);
Int32 lparam = MakeLong(rect.Left - 4, rect.Top + 4);
//clickit
SendMessage( this.Handle, WM_LBUTTONDOWN, 0, lparam);
SendMessage( this.Handle, WM_LBUTTONUP, 0, lparam);
}
static int MakeLong(int LoWord, int HiWord)
{
return (HiWord << 16) | (LoWord & 0xffff);
}
}
.....
.....
//usage - myDataGrid is of type MyDataGrid.
private void button2_Click(object sender, System.EventArgs e)
{
myDataGrid.ClickRowHeader(2);
}
Hope this all helps a bit :D
Paresh
Thanks very much.
i guess it worked !! let me know how it goes.
Paresh
Here is what I end up with:
//Select the newly added row
BindingManagerBase bmb = this.BindingContext[ dataGrid1.DataSource, dataGrid1.DataMember ];
for ( int i = 0; i < bmb.Count; i++ )
{
if (dataGrid1[i, 0].ToString() == newID.ToString() )
{
bmb.Position = i;
break;
}
}
Thanks for your help.
Dion
hmm.. good one. thanks for your tip..
bye