this code gives me error of
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Dim i As Integer = 0
For Each r As DataRow In dt.Rows
DataGridView1.SelectedRows(i).Cells(i) = dt.Rows(i)("Emp_ID")
DataGridView1.SelectedRows(i).Cells(i) = dt.Rows(i)("Emp_Name")
DataGridView1.SelectedRows(i).Cells(i) = dt.Rows(i)("Day_Dat")
i = i + 1
Next
is there a problem clear here ? :(
Loading

Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 8, 2013, 12:47 AM EST
So now what happends if you have total of 2 rows and selected only one row, second time
inside the loop
it will become
DataGridView1.SelectedRows(1).Cells(1) = dt.Rows(1)("Emp_ID")
But there is only one selected row.
Similarly
If there are only 4 cells in a row and you have 10 rows, then again when you say cell(6), it will throw index out of range,
In general, index out of range will come if you dont have any element in that index.
If we get more requirements from you, we can exactly say which line error occurred
ahmed fahd57 reputationPosted Jan 8, 2013, 12:57 AM EST
but the rows not fixed
i have to take the 3 of 4 columns values from sql table
and give them to specified columns in my datagridview
so i tried to do loop, for the rows i retrieved and give them to the datagrid
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 8, 2013, 1:03 AM EST
Because in your current code, you are updating same cell again and again with three columns.
First time you are updating cell 1 with Empid,
second line updating the same cell with empname
like this
ahmed fahd57 reputationPosted Jan 8, 2013, 2:07 AM EST
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 8, 2013, 2:14 AM EST
Check this sample grid example
Now using your code, if i have selected both the records, then first time
i=0
then,
DataGridView1.SelectedRows(0).Cells(0) = dt.Rows(0)("Emp_ID")
table will become
DataGridView1.SelectedRows(0).Cells(0) = dt.Rows(0)("Day_Dat")
So everytime its updating only EmpId column for each row.
So i meant use actual index., which is for empid column, index will be always 0, empname it will be 1, dept will always be 2.
So i asked you to rewrite like
Dim i As Integer = 0
For Each r As DataRow In dt.Rows
DataGridView1.SelectedRows(i).Cells(0) = dt.Rows(i)("Emp_ID")
DataGridView1.SelectedRows(i).Cells(1) = dt.Rows(i)("Emp_Name")
DataGridView1.SelectedRows(i).Cells(2) = dt.Rows(i)("Day_Dat")
i = i + 1
Next
ahmed fahd57 reputationPosted Jan 8, 2013, 2:56 AM EST
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 8, 2013, 2:58 AM EST
Riddhi ValechaRank #794 · 2.9K reputationPosted Jan 8, 2013, 3:12 AM EST
Santosh: Please help me out in Silverlight issues dear.......
In my Silverlight Chat Application, when I debug the login page and redirect to next page, the control again comes back to login page and starts debugging...
I don't know what to do with it....
Please help...
Thanks in advance.,..
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 8, 2013, 3:17 AM EST
Riddhi ValechaRank #794 · 2.9K reputationPosted Jan 8, 2013, 7:01 PM EST
Thanks in advance....
The attached zip is my code....
Riddhi ValechaRank #794 · 2.9K reputationPosted Jan 8, 2013, 9:06 PM EST
Password - riddhi
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 8, 2013, 10:53 PM EST
http://www.c-sharpcorner.com/Forums/Thread/198310/handle-exceptions-and-debugging.aspx
Dinesh Bhoje23 reputationPosted Jan 11, 2013, 12:03 AM EST
sanjeev pandey30 reputationPosted Jan 16, 2013, 9:17 PM EST
maybe yout datatable has less rows data than selected rows of the datagridview.