CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2001
    Posts
    72

    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

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    datagrid's Select
    property

  3. #3
    Join Date
    Jul 2001
    Posts
    72
    Thanks for your help.

    How do I find the newly added row number though?

    Dion

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    just ,

    int nRow = dataGridDetails.BindingContext[dataGridDetails.DataSource, dataGridDetails.DataMember].Count;



    Paresh

  5. #5
    Join Date
    Jul 2001
    Posts
    72
    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

  6. #6
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

  7. #7
    Join Date
    Jul 2001
    Posts
    72
    Thanks very much.

  8. #8
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    i guess it worked !! let me know how it goes.

    Paresh

  9. #9
    Join Date
    Jul 2001
    Posts
    72
    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

  10. #10
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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
  •  





Click Here to Expand Forum to Full Width

Featured