Results 1 to 2 of 2

Thread: Getting the selected index of a table in a datagrid

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Getting the selected index of a table in a datagrid

    Hi everyone. next question. I constructed a table, and added it to a datagrid.

    Pseudocode:

    // Create table.
    // Add values
    DataGrid.DataSource = DataTable;


    It is working fine. I'm getting information to add. However, when I move to delete I want to get the selected index that the user selected. Does anyone know how to do this?

    Where do I get the selectedindex from, the datagrid or the table?

    Jennifer

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Getting the selected index of a table in a datagrid

    The SelectedRowIndex property of the DataGrid indicates the index of the current row in the table's DefaultView, which doesn't necessarily correspond to the index of the same row in the DataTable itself. You could call the Delete method of the DataRowView or the actual DataRow it corresponds to:
    Code:
    myTable.DefaultView[myGrid.CurrentRowIndex].Delete();
    or:
    Code:
    myTable.DefaultView[myGrid.CurrentRowIndex].Row.Delete();
    I believe that you can also use the Postition property of a BindingManagerBase object associated with the DataGrid, but I've never done that myself so I can't offer any specific advice except to perhaps start with the DataGrid.BindingContext property if you're interested.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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