Click here to Skip to main content
Rate this: bad
good
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
Edited 23-Dec-11 0:33am
v3
Comments
PunithaSabareeswari at 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 at 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 at 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();
}
Rate this: bad
good
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.
  Permalink  
Comments
LAKHANP22 at 23-Dec-11 6:34am
   
can u help me ??????????
mark merrens at 23-Dec-11 6:39am
   
I just did!
LAKHANP22 at 23-Dec-11 6:53am
   
thanks mark merrens... thank you very much dear
RaisKazi at 23-Dec-11 8:51am
   
My 5.
Rate this: bad
good
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.
  Permalink  
Comments
LAKHANP22 at 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 at 23-Dec-11 12:50pm
   
Thank you :)
RaisKazi at 23-Dec-11 8:52am
   
My 5.
Drazen Pupovac at 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
0 Sergey Alexandrovich Kryukov 921
1 OriginalGriff 250
2 Volynsky Alex 205
3 Kishore Pogaru 160
4 Richard Deeming 140
0 OriginalGriff 5,640
1 Sergey Alexandrovich Kryukov 4,693
2 CPallini 4,540
3 George Jonsson 3,132
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 23 Dec 2011
Copyright © CodeProject, 1999-2014
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