Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
private void ItemGV_CellClick(object sender, DataGridViewCellEventArgs e)
           if (e.RowIndex >= 0)
               txtItemnum.Text = ItemGV.SelectedRows[0].Cells[0].Value.ToString();
               txtItemname.Text = ItemGV.SelectedRows[0].Cells[1].Value.ToString();
               comboCat.SelectedItem = ItemGV.SelectedRows[0].Cells[2].Value.ToString();
               txtitemprice.Text = ItemGV.SelectedRows[0].Cells[3].Value.ToString();
               //txtemail.Text = UserGV.SelectedRows[0].Cells[4].Value.ToString();

What I have tried:

on click event Iam writing the code foe get the gride values to the textbox

1 solution

We can't tell you "It's there!" because we would need your code running with your data to find out exactly where it is happening, and what values are causing the problem - and you need that before you can start looking at why it's wrong!

All we can do is explain why it's causing a problem. Arrays in C# are a sequence of values accessed via an index: and that index must be between zero and the "number of values minus one". So if you have an array containing five integers for example, then valid indexes are 0, 1, 2, 3, and 4. All other indexes will give an error because the value you are trying to deal with does not exist.
So you can say:
i = arr[0];
i = arr[1];
i = arr[2];
i = arr[3];
i = arr[4];
But not:
i = arr[-1];
i = arr[5];
i = arr[666];

And your code uses several arrays, any of which could be the source of the problem.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it, a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

At a guess, the SelectedItems collection is empty, or there are insufficient columns in the grid, but you need your code running with your data to tell.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer
Brijesh C G 20-Feb-24 2:50am    
Iam going to take the values from the grideview to the textbox's Iam getting error in the last line.

txtitemprice.Text = ItemGV.SelectedRows[0].Cells[3].Value.ToString();

in the table I have a data of
CREATE TABLE [dbo].[TblItem]
[ItemName] VARCHAR(50) NOT NULL,
[ItemPrice] INT NOT NULL
OriginalGriff 20-Feb-24 3:13am    
So what does the debugger show you is in the grid when the error occurs?
Brijesh C G 20-Feb-24 3:17am    
The debugger says that .

This exception was originally thrown at this call stack:
[External Code]
CafeManagment.Item1Form.ItemGV_CellClick(object, System.Windows.Forms.DataGridViewCellEventArgs) in Item1Form.cs
[External Code]
CafeManagment.Program.Main() in Program.cs
Richard Deeming 20-Feb-24 3:39am    
How many SelectedRows does the debugger tell you there are?

And assuming there is at least one, how many Cells does the debugger tell you that SelectedRows[0] has?
OriginalGriff 20-Feb-24 3:55am    
Desk*bang*Desk*bang*Desk*bang* ...

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