Forum guidelines
AuthorQuestion
Get data from DataGridView
Posted By John Doe on 25 May 2009
Hello everyone.I have a problem with reading data from DataGridView to array. Use code: int[,] n = new int[3,19]; for (int i = 1; i <= StartDataView.Rows.Count; i++) { for (int j = 1; j <= StartDataView.Columns.Count; j++) { n[i - 1, j - 1] = (int)StartDataView.Rows[i].Cells[j].Value; } } but all values results is "null",I tried Value.ToString(),but also no result.What I do wrong?
AuthorReply
Vimal Kandasamy
  • 0
  • 0
accepted
Re: Get data from DataGridView
Posted By Vimal Kandasamy on 26 May 2009   Accepted Answer
Hi,
try below code and make sure your datagridview contains integer values only.
n[i - 1, j - 1] = int.Parse(StartDataView.Rows[i].Cells[j].Value.ToString());

it may help you.

Happy to Learn
Vimal Kandasamy
Re: Get data from DataGridView
Posted By John Doe on 26 May 2009  
yeah,it helps,now values is "0" .maybe,I use wrong control or something else.User entering numbers in DataGridView which is bind to BindingSource,i want to get the array of user numbers..what I do wrong?
Re: Get data from DataGridView
Posted By Vimal Kandasamy on 26 May 2009  
using your code,you can store user numbers in an array. is there any other problem in this?...may i know the problem?..

Happy to Learn
Vimal Kandasamy
Re: Get data from DataGridView
Posted By John Doe on 26 May 2009  
I have a table in StringGridView 3x19 ,entering random numbers, but when i debugging,i have a breakpoint on this code, in quickcheck i see StartDataView.Rows[i].Cells[j].Value.ToString()| "StartDataView.Rows[i].Cells[j].Value" is null |string i think problem in a type of cells,i dont get,where in properties i can change it.
Re: Get data from DataGridView
Posted By Vimal Kandasamy on 26 May 2009  
i have some questions

1.how many rows & columns are available in your datagridview?. is it 3 rows and 19 columns?..

2.can you show me some sample content of your cells.?whether all are integer values?.is there  any cell with empty strings("")?.

3.whether you want to retrieve all the rows values or particular rows?.because your for loop starts with 1.so it will read from 2nd rows 2nd columns.


Happy to Learn
Vimal Kandasamy
Re: Get data from DataGridView
Posted By John Doe on 26 May 2009  
1.Yes,I have 3 rows and 19 columns 2.sample data http://rsrc2.bubbleshare.com/media/01/31/2b/b1/4022c785e623df24e686f3ad5779ac00b3d0e456/580x435/rows_580x435.jpg 3.Yes I want to get all data from table change my code: int[,] n = new int[3,19]; for (int i = 0; i <= StartDataView.Rows.Count; i++) { for (int j = 0; j <= StartDataView.Columns.Count; j++) { try { n[i,j] = int.Parse(this.StartDataView.Rows[i].Cells[j].Value.ToString()); } catch { //get exception of "null" } } } P.s: is there any way to paint code on this forum,and add images into post?
Re: Get data from DataGridView
Posted By Vimal Kandasamy on 26 May 2009  
for your sample data...
try this code...

    int[,] n = new int[3,19];
             for (int i = 0; i < (StartDataView.Rows.Count-1); i++)
             {
                 for (int j = 0; j < StartDataView.Columns.Count; j++)
                 {
                     try
                     {
                         n[i, j] = int.Parse(this.StartDataView.Rows[i].Cells[j].Value.ToString());
                     }
                     catch(Exception Ee)
                     { //get exception of "null"
                         MessageBox.Show(Ee.ToString());
                     }
                 }
             } 
if any exception occur, inform me.

Happy to Learn
Vimal Kandasamy

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.