Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code. Please help me.
C#
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
Updated 23-Dec-11 0:33am
v3
Comments
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
con.Open()
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 & "')"
cmd.ExecuteNonQuery()
dtp_trip.Focus()
'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
dgtrip.Rows.Add(row)
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);
con.Open();
SqlCommand cmd = new SqlCommand("pr_deleteblog", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
getBlog();
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 + "\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
}

mark merrens is right.

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

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


I hope this will prevent exception but this is bad way.
 
Share this answer
 
Comments
[no name] 23-Dec-11 8:47am    
thanks Drazen ..i really proud to be member of code project because ...many friend r here , who always help me.in 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.
It look like your count limit could be too high:

C#
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.
 
Share this answer
 
Comments
[no name] 23-Dec-11 6:34am    
can u help me ??????????
R. Giskard Reventlov 23-Dec-11 6:39am    
I just did!
[no name] 23-Dec-11 6:53am    
thanks mark merrens... thank you very much dear
RaisKazi 23-Dec-11 8:51am    
My 5.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900