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" />
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /
    >

    <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. Advertising

  2. 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


    "moni" wrote:

    > 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" />
    > <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    > <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /
    > >

    > <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
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Apr 9, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keithb
    Replies:
    1
    Views:
    2,786
    Teemu Keiski
    Apr 8, 2006
  2. sunil panda

    Lower bound & Upper bound

    sunil panda, Dec 25, 2003, in forum: Java
    Replies:
    9
    Views:
    7,357
    thushara wijeratna
    Oct 7, 2008
  3. H5N1
    Replies:
    0
    Views:
    2,770
  4. Elmo Watson
    Replies:
    0
    Views:
    2,806
    Elmo Watson
    Aug 17, 2006
  5. Replies:
    5
    Views:
    696
Loading...

Share This Page