CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2009
    Posts
    3

    [RESOLVED] Get data from DataGridView

    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";in this case:

    n[i - 1, j - 1] = int.Parse(StartDataView.Rows[i].Cells[j].Value.ToString());

    get "0";

    What I do wrong?

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Get data from DataGridView

    Quote Originally Posted by Zeldan View Post
    Hello everyone.I have a problem with reading data from DataGridView to array. Use code:
    ....

    What I do wrong?
    At first please use codetags,( thats what you do wrong here in this forum ) So please edit your post. Without codetags code losts formatting.
    we do not know if your grid contains integers. Are you sure ? maybe it contains strings, so what contains your grid. Please show a screenshot maybe so we can see
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    May 2009
    Posts
    3

    Re: Get data from DataGridView

    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());
                        }
                    }
                }
    JonnyPoet,thanks you for helping me,not only in understanding of my program mistakes...

    Above the code, I have a StringGridView ,it's bind with BindingSource,user may enter the numbers(with int type) ...but when I debugging ,value is "NULL" ,add some attachments...
    Attached Images Attached Images
    Last edited by Zeldan; May 26th, 2009 at 09:31 AM.

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Get data from DataGridView

    I tested your code in the added project and as you will see it works.
    So the problem must be elsewhere maybe in the databinding itself ?
    Attached Files Attached Files
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    May 2009
    Posts
    3

    Re: Get data from DataGridView

    Thank you very much JonnyPoet,your example helps me to understand my mistakes..i have only binding source in my project,but when i do like in yor example it works!!

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Get data from DataGridView

    Quote Originally Posted by Zeldan View Post
    Thank you very much JonnyPoet,your example helps me to understand my mistakes..i have only binding source in my project,but when i do like in yor example it works!!
    You are welcome
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured