Connecting Tech Pros Worldwide Forums | Help | Site Map

Displaying text from a gridview row bound to SQL server

moni
Guest
 
Posts: n/a
#1: Apr 9 '07
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_SelectedIndexCha nged"
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" /
Quote:
>
<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==?=
Guest
 
Posts: n/a
#2: Apr 9 '07

re: Displaying text from a gridview row bound to SQL server


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:
Quote:
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_SelectedIndexCha nged"
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" /
Quote:
<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
>
>
Closed Thread