0

I am new to asp.net and C# web application development.

My question is: I have a GridView1 which is bound to a data source which is getting data from a SQL Server table.

Gridview1 has these columns:

  • select(default select available in gridview to select row)
  • 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

This is what I am writing, basically the lat and lng variable will be used in my another function:

        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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Deepak Gaur
  • 23
  • 1
  • 5
  • I solved the problem , all i have to do was change the visisbility property of latitude and longitude column to true – Deepak Gaur Feb 20 '15 at 04:29

2 Answers2

0

Make sure that you bind your gridview to a data source before accessing the row collection Gridview.Datasource =yourdatasource Gridview.bind();

MasterBettor
  • 99
  • 1
  • 1
  • 9
0

Try This

lat = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);

Because Gridview Rows and Cells are always start from 0(Zero).

OR

best way is use .findControl() Method of Controls like below.

string lat = ((Label)GridView1.Rows[0].FindControl("labelID")).Text

But, for above code you have to take <asp:label> in <ItemTemplate> in your GridView

prog1011
  • 3,425
  • 3
  • 30
  • 57
  • now following exception occur , Input string was not in a correct format. in my database data type is float for both latitude and longitude field ,if this information add any details to you – Deepak Gaur Feb 19 '15 at 06:15
  • You should debug and see the value in `GridView1.Rows[0].Cells[3].Text` – prog1011 Feb 19 '15 at 06:19
  • `SELECT shedule.latitude, shedule.address, shedule.longitude, shedule.customer, mapicon.icon, shedule.time FROM shedule CROSS JOIN mapicon WHERE (shedule.date = @date) ` this is my sql query of DataSource from where i am fetching data for my GridView1 in troubleshooting tips it is displaying ** when converting a string to DateTime , Parse the string to take the date before putting each variable into the DateTime object** , – Deepak Gaur Feb 19 '15 at 06:40