473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Displaying text from a gridview row bound to SQL server

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" /
>
<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

Apr 9 '07 #1
1 5467
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_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" /
<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

Apr 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bigbrorpi | last post by:
Hi I have a FormView that contains a GridView. The GridView is populated from an ObjectDataSource. Two of the columns in the GridView are not bound, and the values used in Update mode come from...
0
by: Deepak Palkar | last post by:
Hi I have Gridview and DetailView both on one page. I am showing the list in the Gridview and the particular record in DetailView. the problem is When i EDIT on the DetailView and try to UPDATE...
0
by: moni | last post by:
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"...
1
by: weird0 | last post by:
I have created a GridView and dynamically added data to it by creating a DataTable(as advised) and bound it with a reader. Then, assigned the DataSource of GridView to DataTable. But even that aint...
5
by: =?Utf-8?B?QWRhciBXZXNsZXk=?= | last post by:
Hi All, I have a GridView inside the EditItemTemplate of a FormView. Both FormView and GridView are data bound using an ObjectDataSource. When the FormView's ObjectDataSource object has a...
10
by: gnewsgroup | last post by:
I've googled and tried various approaches, but could not resolve this problem. The article at MSDN: Displaying Images in a GridView Column only presents a simple case where all data (including the...
51
by: kjewell23 | last post by:
Hello, I'm new to this asp.net 2.0 I need help?? My database is coming from AS400 which uses odbc Commands. i have data in the dataset but nothing showing in my gridview. However the data does show...
1
by: | last post by:
How do I add static text to a bound field in a gridview? For instance, I want the following label in a gridview to be displayed as "Smith: Exempt" "Jones: Exempt" etc. <asp:Label ID="Label1"...
1
by: Dave | last post by:
I'm having problems getting the GridView to reliably display a large amount of data (50,000+ rows). I am working my way through the excellent book “Real World ASP.NET Best Practices” by Farhan...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.