简体   繁体   English

如何在页面加载时以编程方式从GridView1的第一行第4列单元格中选择数据并将其保存到C#asp.net中的变量

[英]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有以下列:

  • select(default select available in gridview to select row) 选择(默认选择gridview中可用以选择行)
  • time 时间
  • customer 顾客
  • address 地址
  • longitude (visible = false) 经度(可见=假)
  • latitude (visible = false) 纬度(可见=假)

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 RowsCells始终从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.

相关问题 选中从gridview1到gridview2的所选行ASP.NET C# - checkboxed selected rows from gridview1 to gridview2 ASP.NET C# Asp.Net C#GridView第三列中的第一个单元格为空,但数据存在 - Asp.Net C# GridView first cell in third column is empty but data exists 从 GridView asp.net c# 中选择一个单元格值 - Select a cell value from GridView asp.net c# 如何在ASP.NET C#中将多个gridview行数据保存到数据库 - How to save multiple gridview row data to database in asp.net c# 如何从ASP.NET C#中的GridView获取单击的超链接行相邻单元格值 - How to get clicked hyperlink row adjacent cell value from GridView in ASP.NET C# 当在ASP.NET C#中发生页面加载时,如何在gridview中按行删除数据? - How to delete data as rowwise in gridview when the page load occur in asp.net c#? 如何将 GridView 中的数据保存和更新到 ASP.Net C# 中的数据库? - how to Save and Update data from GridView to database in ASP.Net C#? 我如何 select 并将数据行保存在表格中? C# ASP.NET MVC 5 - How can I select and save data row in a table? C# ASP.NET MVC 5 使用C#代码在ASP.NET GridView中选择一行 - Select a row in ASP.NET GridView with C# code ASP.Net C# 在 GridView 中选择行而不更新整个页面 - ASP.Net C# Select Row in GridView without Updating the whole Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM