Displaying text from a gridview row bound to SQL server

Discussion in 'ASP .Net' started by moni, Apr 9, 2007.

  1. moni

    moni Guest

    Hi,

    I was hoping to know how I could get the text value of a gridview row.
    My code is this way:
    <asp:GridView ID="GridView1" runat="server"
    AllowSorting="True" AutoGenerateColumns="False"
    BackColor="#8080FF"
    BorderColor="#0000C0" BorderStyle="None"
    BorderWidth="1px" CellPadding="3" CellSpacing="2" Font-
    Size="Large" Height="192px" Width="243px"
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
    OnRowCommand="GridView1_RowCommand">
    <Columns>
    <asp:ButtonField DataTextField="Name" HeaderText
    ="Select" />


    </Columns>
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
    ForeColor="White" />
    <HeaderStyle BackColor="DarkBlue" Font-Bold="True"
    ForeColor="White" />
    </asp:GridView>

    //To bind data
    SqlConnection conn = new SqlConnection(connString);
    conn.Open();

    String sqlstr = null;
    sqlstr = "Select Name,Address,ZipCode from restaurants
    where ZipCode='" + TextBox1.Text + "'";

    /*
    //get values of the gridview items displayed as
    addresses
    //call the javascript function for adding each point
    as an overlay

    */
    SqlCommand comm = new SqlCommand(sqlstr, conn);

    GridView1.DataSource =
    comm.ExecuteReader(CommandBehavior.CloseConnection);


    GridView1.DataBind();

    GridView1.Focus();

    conn.Close();

    This is being able to bind and display data properly. But on a row
    select, it is displaying the correct index , but not the text.

    protected void GridView1_RowCommand(object sender,
    GridViewCommandEventArgs e)
    {
    Label4.Text = GridView1.SelectedRow.RowIndex.ToString();

    }

    Even
    GridView1.SelectedRow.Cells[0].Text;

    gives me a blank label.

    Any help will be appreciated. Thanks
     
    moni, Apr 9, 2007
    #1
    1. Advertisements

  2. moni

    Guest Guest

    Seems you're getting the cell's text not the button's. To get the buttons
    text (which is bound the "Name" column, try this:

    IButtonControl button = ((IButtonControl)
    GridView1.SelectedRow.Cells[0].Controls[0]).Text;

    Hope this helps
    --
    Milosz


     
    Guest, Apr 9, 2007
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.