Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7651497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:31:19+00:00 2026-05-31T11:31:19+00:00

I am using an asp gridview element to do a search on the database

  • 0

I am using an asp gridview element to do a search on the database and return some data using

    <asp:GridView ID="storedRecordsGrid" AutoGenerateSelectButton="true"  emptydatatext="No data    available." runat="server">

So i have a gridview that gets popolated and each row has its own select event. When the user selects a row I want to use the data in that row to populate texboxes on my page.

    protected void OnRowCommand(GridViewCommandEventArgs e)
        { 

        }

So I have this method and i have my textboxes but when the select even fires I am not sure how the data is stored for each colums of that selected row. what i want to have is something like.

    protected void OnRowCommand(GridViewCommandEventArgs e)
        { 
         mytextbox.text=datagridcolumn.value   
        }

I am using .net 2.0
and my c# code as of right now looks like this.

private DataTable fillGrid(string dComplainantFName,string dComplainantlName)
   {
        string server = "";
        using (SqlConnection cnx = new SqlConnection(server))
        using (SqlCommand cmd = new SqlCommand("ComplaintReportLookUp", cnx) { CommandType = CommandType.StoredProcedure })
        using (DataTable dt = new DataTable())
        {
            cmd.Parameters.AddWithValue("@fName", dComplainantFName);
            cmd.Parameters.AddWithValue("@lName", dComplainantlName);
            try
            {
                cnx.Open();
                dt.Load(cmd.ExecuteReader());
                cnx.Close();
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing MyProcedureName.", ex);
            }
        }

    }
   protected void executeGrid(object sender, EventArgs e)
        {
            storedRecordsGrid.DataSource = this.fillGrid(dComplainantFName.Text,dComplainantLName.Text);
            storedRecordsGrid.DataBind();
        }
    protected void OnRowCommand(GridViewCommandEventArgs e)
        { 

        } 

Thank you for your help


Thanks Tim for the help this is what ended up working for me.

    protected void gridSelectedIndexChanged(Object sender, EventArgs e)
    {
    var row = ((GridView)sender).SelectedRow;
    //var cell1Text = row.Cells[0].Text; // should be your autogenerated select cell 
    dComplainantFName.Text = row.Cells[1].Text.Replace("&nbsp;", ""); // Text in column 1 (the first column of your DataTable) 
    dComplainantLName.Text = row.Cells[2].Text.Replace("&nbsp;", ""); // Text in column 2 (the second column of your DataTable) 
    dComplainantMName.Text = row.Cells[3].Text.Replace("&nbsp;", "");
    dComplainantAddress.Text = row.Cells[4].Text.Replace("&nbsp;", "");
    dComplainantCity.Text = row.Cells[5].Text.Replace("&nbsp;", "");
    dComplainantState.Text = row.Cells[6].Text.Replace("&nbsp;", "");
    dComplainantZip.Text = row.Cells[7].Text.Replace("&nbsp;", "");
    dComplainantPhone.Text = row.Cells[8].Text.Replace("&nbsp;", "");
    storedRecordsGrid.Visible = false;
    } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:31:20+00:00Added an answer on May 31, 2026 at 11:31 am

    Use the GridView’s SelectedIndexChanged event, in the other events like RowCommand or SelectedIndexChanging the SelectedRow property is null. It’s also the appropriate event for your requirement:

    protected void Grid_SelectedIndexChanged(Object sender, EventArgs e)
    {
        var row = ((GridView)sender).SelectedRow;
        var cell1Text = row.Cells[0].Text; // is your autogenerated select cell(String.Empty)
        var cell2Text = row.Cells[1].Text; // Text in column 2 (the first column of your DataTable)
        var cell3Text = row.Cells[2].Text; // Text in column 3 (the second column of your DataTable)
        TextBox1.Text = cell2Text; // etc.
    }
    

    Note: If you wouldn’t have set AutoGenerateSelectButton to true and have used TemplateFields, you ought to use FindControl on the SelectedRow to get a reference to your controls.

    If you still insist on using RowCommand, you could get the selected row from the CommandArgument:

    protected void Grid_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            var rowIndex    = int.Parse(e.CommandArgument.ToString())
            var selectedRow = ((GridView)sender).Rows[rowIndex];
            var cell1Text   = selectedRow.Cells[0].Text; // is your autogenerated select cell(String.Empty)
            var cell2Text   = selectedRow.Cells[1].Text; // Text in column 2 (the first column of your DataTable)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im using ASP.NEt to make a simple database query and return JSON formated data.
I am using ASP.Net 2.0. I am using a gridview component over some data
Using ASP.NET VB, I have a form with some text boxes and a Gridview.
Given a GridView control defined like this: <asp:GridView ID=AttachedFilesGridView runat=server AllowSorting=true AllowPaging=false CellPadding=5 AutoGenerateColumns=false
I'm using an ObjectDataSource to sort/page/filter as below: <asp:ObjectDataSource ID=odsCompaniesIndex runat=server EnablePaging=true SelectMethod=GetCompaniesSubset StartRowIndexParameterName=startRowIndex
I am using an Asp:GridView with AutoGenerateColumns property set to true to display all
I'm using LINQ to Entities My GridView is the following : <asp:GridView ID=GridView1 runat=server
I am using ASP.Net 2.0 and trying to display transformed xml data using GridView
I have asp:GridView displaying client requests using asp:SqlDataSource . I want to limit displayed
I am using an ASP.NET GridView control, created dynamically with C# on a SharePoint

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.