Datagrid using loop.

Inserting values in datagrid using loop.

Page 1 of 1

8 Replies - 3037 Views - Last Post: 11 November 2010 - 02:53 AM Rate Topic: ****- 1 Votes

#1 Fhahroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-November 10

Datagrid using loop.

Posted 08 November 2010 - 01:43 PM

I have two textboxes txt1 and txt2 respectively, what I want to do is on a button click I want to enter there values in a datagrid one by one in one column.

For example if I have entered 2 in txt1 and 2 in txt2 also then datagrid should have a values like:
Column Name
1
1
2
2

I've used a loop to do this but no results. The values are displayed but not as I want.

Here's my code.

[b]int index = 0;
int index2 = 0;
int j = 1;
for (int i = 1; i <= txt1.Text; i++)
{
dataGrid1.Rows.Add();
dataGrid1.Rows[index].Cells["Column1"].Value = "" + i;
index++;
   if (j <= txt2.Text)
   {
     dataGrid1.Rows.Add();
     dataGrid1.Rows[index2].Cells["Column1"].Value = "" + j;
     index2++;
     j++
   }
}[/b]



What am I doing wrong?
Any help will be really appreciated. Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Datagrid using loop.

#2 Fhahroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-November 10

Re: Datagrid using loop.

Posted 08 November 2010 - 01:54 PM

Is there any one to help....Please???
Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Datagrid using loop.

Posted 08 November 2010 - 10:30 PM

I'd suggest doing some research on google about datagrids, here's a start
MSDN DataGrid

and on Converting strings to integers

MSDN Convert Class

int index = 0;
int index2 = 0;
int j = 1;
for (int i = 1; i <= txt1.Text; i++) // How can you compare int i to string txt1.Text? might need to convert the string
{
dataGrid1.Rows.Add();
dataGrid1.Rows[index].Cells["Column1"].Value = "" + i; // DataGrids don't have a Row property
index++;
   if (j <= txt2.Text) // Can't compare an int to a string
   {
     dataGrid1.Rows.Add();
     dataGrid1.Rows[index2].Cells["Column1"].Value = "" + j;
     index2++;
     j++
   }
}



Edit: In the future try to include what errors you are getting and some of the steps you have tried to fix it. Wouldn't want people to think you're just trying to get someone to give you the code!

This post has been edited by Nakor: 08 November 2010 - 10:31 PM

Was This Post Helpful? 0
  • +
  • -

#4 Fhahroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-November 10

Re: Datagrid using loop.

Posted 09 November 2010 - 02:27 AM

I'm sorry here's my modified code.

int index = 0;
int index2 = 0;
int j = 1;
int txt1 = Convert.ToInt32(txt1.Text);
int txt2 = Convert.ToInt32(txt2.Text);

for (int i = 1; i <= txt1; i++)
{
   dataGrid1.Rows.Add();
   dataGrid1.Rows[index].Cells["Column1"].Value = "" + i; //Datagrids don't have a Row property, but they do have Rows property.
   index++;
     if (j <= txt2)
     {
       dataGrid1.Rows.Add();
       dataGrid1.Rows[index2].Cells["Column1"].Value = "" + j;
       index2++;
       j++
     }
}


I'm having no errors but the wrong output. For example if I enter 2 in txt1 and 3 in txt2 then instead of having output like:

Column1
1 //From txt1.
1 //From txt2.
2 //From txt1.
2 //From txt2.
3 //From txt2.

I'm having:

Column1.
1 //From txt1.
2 //From txt1.
3 //From txt2.

My code is working but the output is incorrect. So I wanna know what am I doing wrong which line of code is incorrect? I've searched every forum and tutorials on datagrid but did not achieve my goal. I'm coding on C# using visual studio 2008. Please help Thank You.
Was This Post Helpful? 0
  • +
  • -

#5 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 216
  • Joined: 18-May 10

Re: Datagrid using loop.

Posted 09 November 2010 - 03:34 AM

You're using the wrong indices. The first loop does the following:

Add row for txt1
Change value for row[index] -> index is 0 -> Row 0 is changed -> Thats ok
index++;
Add row for txt2
Change value for row[index2] -> index2 is 0 -> Row 0 is changed -> Thats bad (should be 1)
index2++;

Second loop:

Add row for txt1
Change value for row[index] -> index is 1 -> Row 1 is changed -> Thats bad (should be 2)
index++;
Add row for txt2
Change value for row[index2] -> index2 is 1 -> Row 1 is changed -> Thats bad (should be 3)
index2++;

I hope you will find the solution yourself

This post has been edited by elbielefeld: 09 November 2010 - 03:35 AM

Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Datagrid using loop.

Posted 09 November 2010 - 05:48 AM

View PostFhahroz, on 09 November 2010 - 04:27 AM, said:

I'm having no errors but the wrong output. For example if I enter 2 in txt1 and 3 in txt2 then instead of having output like:

Column1
1 //From txt1.
1 //From txt2.
2 //From txt1.
2 //From txt2.
3 //From txt2.

I'm having:

Column1.
1 //From txt1.
2 //From txt1.
3 //From txt2.

My code is working but the output is incorrect. So I wanna know what am I doing wrong which line of code is incorrect? I've searched every forum and tutorials on datagrid but did not achieve my goal. I'm coding on C# using visual studio 2008. Please help Thank You.


Hello, I'm seeing no discernible pattern here for adding rows of information. Can you tell us in what pattern you'd like to insert into the DataGridView from the textboxes?

Be specific and leave nothing out and then we'll be able to help out.
Was This Post Helpful? 0
  • +
  • -

#7 Fhahroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-November 10

Re: Datagrid using loop.

Posted 10 November 2010 - 01:23 AM

View PostSergio Tapia, on 09 November 2010 - 04:48 AM, said:

View PostFhahroz, on 09 November 2010 - 04:27 AM, said:

I'm having no errors but the wrong output. For example if I enter 2 in txt1 and 3 in txt2 then instead of having output like:

Column1
1 //From txt1.
1 //From txt2.
2 //From txt1.
2 //From txt2.
3 //From txt2.

I'm having:

Column1.
1 //From txt1.
2 //From txt1.
3 //From txt2.

My code is working but the output is incorrect. So I wanna know what am I doing wrong which line of code is incorrect? I've searched every forum and tutorials on datagrid but did not achieve my goal. I'm coding on C# using visual studio 2008. Please help Thank You.


Hello, I'm seeing no discernible pattern here for adding rows of information. Can you tell us in what pattern you'd like to insert into the DataGridView from the textboxes?

Be specific and leave nothing out and then we'll be able to help out.



Well i've already mentioned the pattern above, how ever i want the output in the datagrid in this format:

Column1.
1 // this 1 will come from txt1 i've entered 2 in txt1.
1 // this 1 will come from txt2 txt2 also have 2 in it.
2 // now this 2 should come from txt1.
2 // and this 2 should come from txt2..


Thats the output i want....u can check out my loop.
Was This Post Helpful? 0
  • +
  • -

#8 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Datagrid using loop.

Posted 10 November 2010 - 06:02 AM

Quote

1 // this 1 will come from txt1 i've entered 2 in txt1.


This doesn't make sense. If you entered '2' in txt1 control, why is there a '1' value there? Be clearer please
Was This Post Helpful? 0
  • +
  • -

#9 Fhahroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-November 10

Re: Datagrid using loop.

Posted 11 November 2010 - 02:53 AM

I found the almost solution this time I've used two numeric updown controls. and i modified my code to this.

int j = 1, index = 0, index2 = 1;
for(int i = 1; i<=num1.Value; i++)
{
  dataGrid1.Rows.Add();
  dataGrid1.Rows[index].Cells["Column1"].Value = ""+ i;
                    
  if (j <= num2.Value)
  {
    dataGrid1.Rows.Add();
    dataGrid1.Rows[index2].Cells["Column1"].Value =""+j;
    j++;
    index2+=2;                 
  }
index+=2;
}


Now if i enter 2 in num1 and 2 in num2 it works fine giving me the out after looping through the values of numeric updown values like:

Column1.
1 //From num1.
1 //From num2.
2 //From num1.
2 //From num2.

Now the problem arises if i enter 2 in num1 and 3 in num2 then instead of giving me the output it shows me the exception index out of bounds. Please suggest me some help. Thanks.

This post has been edited by Fhahroz: 11 November 2010 - 02:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1