Click here to Skip to main content
Sign Up to vote bad
good
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:29
Edited 23 Dec '11 - 0:33

Comments
PunithaSabareeswari - 16 May '12 - 1:11
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:09
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:01
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(); }

2 solutions

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 - 23 Dec '11 - 6:34
can u help me ??????????
mark merrens - 23 Dec '11 - 6:39
I just did!
LAKHANP22 - 23 Dec '11 - 6:53
thanks mark merrens... thank you very much dear
RaisKazi - 23 Dec '11 - 8:51
My 5.
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 - 23 Dec '11 - 8:47
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:50
Thank you :)
RaisKazi - 23 Dec '11 - 8:52
My 5.
Drazen Pupovac - 23 Dec '11 - 12:50
Thank you.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Arun Vasu 394
1 OriginalGriff 370
2 Sergey Alexandrovich Kryukov 313
3 Tadit Dash 213
4 Maciej Los 191
0 Sergey Alexandrovich Kryukov 9,955
1 OriginalGriff 7,589
2 CPallini 4,028
3 Rohan Leuva 3,422
4 Maciej Los 2,949


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 23 Dec 2011
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid