|
-
March 11th, 2003, 11:41 AM
#1
Change Current Cell or Row programatically
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
-
March 11th, 2003, 12:44 PM
#2
datagrid's Select
property
-
March 11th, 2003, 12:57 PM
#3
Thanks for your help.
How do I find the newly added row number though?
Dion
-
March 11th, 2003, 01:06 PM
#4
just ,
int nRow = dataGridDetails.BindingContext[dataGridDetails.DataSource, dataGridDetails.DataMember].Count;
Paresh
-
March 11th, 2003, 02:56 PM
#5
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
-
March 11th, 2003, 09:05 PM
#6
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 
Paresh
-
March 12th, 2003, 09:16 AM
#7
-
March 12th, 2003, 02:28 PM
#8
i guess it worked !! let me know how it goes.
Paresh
-
March 12th, 2003, 02:36 PM
#9
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
-
March 12th, 2003, 02:38 PM
#10
hmm.. good one. thanks for your tip..
bye
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|