Click here to Skip to main content
12,398,557 members (59,399 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
Here is my code. Please help me.
string day = dataGridView1.Rows[0].Cells[0].Value.ToString();//DateTime.Now.DayOfWeek.ToString();

           dataGridView1.Rows[0].Cells[1].Value = (Convert.ToDateTime(day.ToString())).DayOfWeek.ToString();
           for (int i = 1; i < 10; i++)

               DateTime dtd = Convert.ToDateTime(day).Date;
               dtd = dtd.AddDays(7);
               dataGridView1.Rows[i].Cells[0].Value = dtd;
               DateTime date = dtd;
               dataGridView1.Rows[i ].Cells[1].Value = (Convert.ToDateTime(date.ToString())).DayOfWeek.ToString();
I get "error=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".

Please help me.

Thanks in advanced
Posted 23-Dec-11 0:29am
Updated 23-Dec-11 0:33am
PunithaSabareeswari 16-May-12 1:11am
Private Sub txt_unload_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_unload.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
cmd.Connection = con
cmd.CommandText = "insert into Trip(TDate,TFrom,Tto,Party,KM,Quantity,Rate,Type,FAmt,Rcv,Balance,TLoad,TUnload,TSDate,VehNo)values('" & dtp_trip.Value & "','" & cb_from.Text & "','" & cb_to.Text & "', '" & cb_party.Text & "', " & txt_km.Text & ", " & txt_qty.Text & "," & txt_rate.Text & ",'" & cb_mkqty.Text & "', " & txt_freight.Text & "," & txt_recv.Text & "," & txt_bal.Text & ", " & txt_load.Text & "," & txt_unload.Text & ",'" & dtp_date.Value & "','" & cb_vno.Text & "')"
'ElseIf e.KeyChar = ChrW(Keys.Tab) Then
''Dim cmdstr As String
'cmdstr = "select * from Trip"
'da = New SqlDataAdapter(cmdstr, con)
'ds = New DataSet()
'da.Fill(ds, "trip")
'dgtrip.DataSource = ds.Tables(0)
Dim row As New DataGridViewRow
Dim cell As New DataColumn
row = New DataGridViewRow
'row.Cells(0).Value = dtp_trip.Value
row.Cells(1).Value = cb_from.Text
row.Cells(2).Value = cb_to.Text
row.Cells(3).Value = cb_party.Text
row.Cells(4).Value = txt_km.Text
row.Cells(5).Value = txt_qty.Text
row.Cells(6).Value = txt_rate.Text
row.Cells(7).Value = cb_mkqty.Text
row.Cells(8).Value = txt_freight.Text
row.Cells(9).Value = txt_recv.Text
row.Cells(10).Value = txt_bal.Text
row.Cells(11).Value = txt_load.Text
row.Cells(12).Value = txt_unload.Text
End If
End Sub

I get "error=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".
help me.....
apitdon 4-Sep-12 2:09am
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
int blogid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["blogid"].ToString());
string blogtype = GridView1.DataKeys[e.RowIndex].Values["blogtype"].ToString();
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("pr_deleteblog", con);
int result = cmd.ExecuteNonQuery();
if (result == 1)
lblresult.ForeColor = Color.Red;
lblresult.Text = blogtype + " details deleted successfully";

I get "error=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".

please help me..thanks in advance..
deepak tiwari11 25-Feb-13 6:01am
protected void lnkDownload_Click(object sender, EventArgs e)
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
string filePath = Grd_jobseeker_Detail.DataKeys[gvrow.RowIndex].Value.ToString();
Response.ContentType = "docx/doc";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It look like your count limit could be too high:

i < 10

Reduce that to an appropriate amount and try again.

You could also pre-determine the number of rows you are likely to find and use that instead so that you don't need to hard code any limits.
LAKHANP22 23-Dec-11 6:34am
can u help me ??????????
mark merrens 23-Dec-11 6:39am
I just did!
LAKHANP22 23-Dec-11 6:53am
thanks mark merrens... thank you very much dear
RaisKazi 23-Dec-11 8:51am
My 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

mark merrens is right.

to prevent error you can add in for loop if condition:

if (this.dataGridView1.RowCount == i)
   break; // or return;

I hope this will prevent exception but this is bad way.
LAKHANP22 23-Dec-11 8:47am
thanks Drazen ..i really proud to be member of code project because ...many friend r here , who always help my organisation there no any senior to help me ....but...friends like u i never fill alone .....thanks to all who help me and other.......... :)
Drazen Pupovac 23-Dec-11 12:50pm
Thank you :)
RaisKazi 23-Dec-11 8:52am
My 5.
Drazen Pupovac 23-Dec-11 12:50pm
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 23 Dec 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100