[英]How to programatically select the data from GridView1's first row 4th column cell on page load and save it to a variable in C# asp.net
I am new to asp.net and C# web application development. 我是asp.net和C#Web应用程序开发的新手。
My question is: I have a GridView1
which is bound to a data source which is getting data from a SQL Server table. 我的问题是:我有一个
GridView1
绑定到从SQL Server表获取数据的数据源。
Gridview1
has these columns: Gridview1
有以下列:
This data is also filtered and sorted by a drop down list date 此数据也会按下拉列表日期进行过滤和排序
What I want to do is when my page load the value of latitude and longitude column first row value get assigned to my variable lat and lng 我想要做的是当我的页面加载纬度和经度列的第一行值被赋值给我的变量lat和lng
This is what I am writing, basically the lat and lng variable will be used in my another function: 这就是我写的,基本上lat和lng变量将在我的另一个函数中使用:
Double lat, lng;
lat = Convert.ToDouble(GridView1.Rows[1].Cells[4].Text);
lng = Convert.ToDouble(GridView1.Rows[1].Cells[5].Text);
GLatLng latlong = new GLatLng(lat, lng);
GMap1.setCenter(latlong, 10);
The following exception is thrown: 抛出以下异常:
Index was out of range.
指数超出范围。 Must be non-negative and less than the size of the collection.
必须是非负数且小于集合的大小。
The gridview data is never null it always has some data gridview数据永远不会为null,它总是有一些数据
在访问行集合Gridview.Datasource = yourdatasource Gridview.bind()之前,请确保将gridview绑定到数据源。
Try This 尝试这个
lat = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
Because Gridview Rows
and Cells
are always start from 0 (Zero). 因为
Gridview Rows
和Cells
始终从0 (零)开始。
OR 要么
best way is use .findControl() Method of Controls
like below. 最好的办法是用.findControl()方法的
Controls
像下面。
string lat = ((Label)GridView1.Rows[0].FindControl("labelID")).Text
But, for above code you have to take <asp:label>
in <ItemTemplate>
in your GridView
但是,对于上面的代码,您必须在
GridView
中的<ItemTemplate>
中使用<asp:label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.