CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2007
    Posts
    6

    How to get text selected row of grid view

    how to get the text of a particular cell of selected row in grid view.

  2. #2
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: How to get text selected row of grid view

    Code:
    Dim str As String = myGridView.SelectedRow.Cells(index).Text
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  3. #3
    Join Date
    Dec 2007
    Posts
    6

    Re: How to get text selected row of grid view

    Thank u for your reply
    same thing i have tried.like........

    protected void gv1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
    string strText = gv1.SelectedRow.Cells[0].Text.ToString();

    }
    but it is giving error like "object reference not set to an instance of object".
    in which event i should write this code.............

  4. #4
    Join Date
    Dec 2007
    Posts
    6

    how to get text of a particular cell of selected row in grid view

    here is my code........
    protected void myGridview_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
    string strText = myGridview.SelectedRow.Cells[0].Text.ToString();

    }
    it is giving error like "object reference not set to an instance of object".
    in which event i should write this code.............

  5. #5
    Join Date
    Nov 2003
    Posts
    2,185

    Re: how to get text of a particular cell of selected row in grid view

    You should first check if the selected row is a valid object.

    Code:
    // Get the row
    Row selectedRow = myGridView.SelectedRow;
    
    // Check if we have a valid row
    if (selectedRow != null)
    {
        // Get the first column (only directly call to a column index if you are SURE it exists)
        string text = selectedRow.Cells[0].Text;
    }
    Disclaimer:
    I wrote this out of my head, so it's possible it contains some typos.

  6. #6
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: How to get text selected row of grid view

    Which reference is null? gv1? gv1.SelectedRow? gv1.SelectedRow.Cells? gv1.SelectedRow.Cells[0]?

    Also, why are you calling the ToString method of a String?
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: How to get text selected row of grid view

    Code:
    .... gv1.SelectedRow.Cells[0]?
    Why are you using zero instead of the index of the column you need to get ? or do you want to get index zero ??
    The other point is: Using this web control the event SelectedIndexChanging you are using is called before the change occurs. So in the first occurance of it this event will not have a selected row so it is null and you will have an Exception thrown. On the other side,you cannot use SelectedRow.Cells as you will get no or a former selection which is of no use. Using this event you need to do
    Code:
    gv1.Rows[e.NewSelectedIndex].Cells[clumnIndex].Text
    otherwise it wouldn't work
    This event is used before changing the selection !! So normally you use it for checking and eventually cancelling the selections change. If you want to use selectedRow you need to use the SelectedIndexChanged event
    Hope this ends your troubles
    Last edited by JonnyPoet; January 7th, 2008 at 02:02 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: how to get text of a particular cell of selected row in grid view

    Please dont double Post. the solution I have done in your other post. You are using the wrong event ! See your other post for why and how to handle that.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

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