feurich Posted February 26, 2009 Posted February 26, 2009 Hi there, I need to store all the values of the first row of a datagridview in to an array. I have tried a lot but havent managed to do this. Can someone help me on the way. Thanks, Quote Trust the Universe
feurich Posted February 26, 2009 Author Posted February 26, 2009 I have this....!! :confused: int i = 0; string[] cellValues = null; // Only take the first row for the search values foreach (DataGridViewCell dataCell in DataGridView1.SelectedRows[0].Cells) { cellValues[i] = dataCell.Value.ToString(); i++; } Quote Trust the Universe
Administrators PlausiblyDamp Posted February 26, 2009 Administrators Posted February 26, 2009 You would need to make the array big enough to hold each item. Something like string[] cellValues = new string[DataGridView1.SelectedRows[0].Cells.Count] that is probably a bit wrong as I don't have VS handy to test it. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
feurich Posted February 26, 2009 Author Posted February 26, 2009 The problem here is that i get an error on the SelectedRows[0].Cells.Count part of the statement. It says: System.ArgumentoutOfRangeException in the for loop. Actual value was null. But I am in the grid and there are 3 cells filled with data in the first row... Quote Trust the Universe
feurich Posted February 26, 2009 Author Posted February 26, 2009 Hi There, It's not the DataGridView1.SelectedRows[0] property that was needed but the DataGridView1.Rows[0].Cells.Count. That solved this problem on the the next one. Thanks PlausiblyDamp for showing the way.. :) Quote Trust the Universe
feurich Posted February 26, 2009 Author Posted February 26, 2009 Hi There, It's not the DataGridView1.SelectedRows[0] property that was needed but the DataGridView1.Rows[0].Cells.Count. That solved this problem on the the next one. Thanks PlausiblyDamp for showing the way.. :) // Only take the first row for the search values string[] cellValues = new string[DataGridView1.Rows[0].Cells.Count]; for (int i = 0; i < this.DataGridView1.Rows[0].Cells.Count; i++) { cellValues[i] = DataGridView1.Rows[0].Cells[i].Value.ToString(); } Quote Trust the Universe
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.