Click here to Skip to main content
16,016,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi my freands

i cant add more than 1 row in my datagridview !

i have 2 button and 1 datagridview with 2 column : col01 and col02 .

when i click on button1 i want to add new row and col01 = "test name" (for example)

and when i click on button2 i want to : col02 of that row = "1111"


and again repeat this and add another rows
Posted
Comments
Kornfeld Eliyahu Peter 26-Jan-15 12:10pm    
itman2 26-Jan-15 12:16pm    
private void button1_Click(object sender, EventArgs e)
{

dgv1.Rows[dgv1.Rows.Count-1].Cells[0].Value = "test";

}
private void button2_Click(object sender, EventArgs e)
{
dgv1.Rows[dgv1.Rows.Count - 1].Cells[1].Value = "1111";
}
Kornfeld Eliyahu Peter 26-Jan-15 12:22pm    
Read here to see if you can go further: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows(v=vs.110).aspx
PIEBALDconsult 26-Jan-15 12:53pm    
You add it to the data source.

Sounds kind of interesting, and if you have to repeat the process but only if both buttons were pressed.

I would think button 2 is disabled until 1 is clicked, then when hit button 1 is disabled, button 2 enabled. When button 2 is clicked, button 1 is enabled, and button 2 is again disabled.

The alternative being that you would need to keep track of which row did not get second column filled in and figure out how you would handle that, Or just not add anything and move on, don't disable anything..

Of course make sure you subscribe to your event with each button :)

C#
private void button1_Click(object sender, EventArgs e){
    if (null != dgv1) {
        dgv1.Rows.Add();
        dgv1.Rows[dgv1.Rows.Count - 1].Cells[0].Value = "test name";
        button2.Enabled = true;
        button1.Enabled = false;
    }
}

private void button2_Click(object sender, EventArgs e) {
    if (null != dgv1) {
        dgv1.Rows[dgv1.Rows.Count - 1].Cells[1].Value = "1111";
        button2.Enabled = false;
        button1.Enabled = true;
    }
}
 
Share this answer
 
v2
thanks but it was very simple ! i solved it !
now i can create a Sales Factor with this solution !!! :-) ;-)

C#
int i =0 
private void button1_Click(object sender, EventArgs e)
{
dg1.Rows.Add("test nsme");
}

private void button2_Click(object sender, EventArgs e)
{
            if (row == 0)
            {
                dg1.Rows[0].Cells[1].Value = "test number";
            }
            else
            {
                i = i + 1;
                dg1.Rows[i].Cells[1].Value = "test number";
               
            }
row = Convert.ToInt32(dg1.Rows.Count.ToString()) ;
}
 
Share this answer
 

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