如何在C#中向Datagridview添加新行 [英] How Can I Add New Row To Datagridview In C#

查看:78
本文介绍了如何在C#中向Datagridview添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的freands



i在我的datagridview中添加超过1行!



i有2按钮和1个datagridview有2列:col01和col02。



当我点击button1我想添加新行和col01 =测试名称(例如)



当我点击button2时我想:该行的col02 =1111





并再次重复此操作并添加另一行

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

推荐答案

听起来很有意思,如果你必须重复这个过程,但只有两者都有按下按钮。



我认为按钮2被禁用直到点击1,然后当按下按钮1被禁用时,按钮2被启用。单击按钮2时,按钮1启用,按钮2再次禁用。



另一种方法是你需要跟踪哪一行没有填写第二列,并弄清楚你将如何处理,或者只是不添加任何东西并继续前进,不要禁用任何东西..



当然请确保您使用每个按钮订阅您的活动:)



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 :)

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;
    }
}


谢谢,但这很简单!我解决了!

现在我可以通过这个解决方案创建销售因素! :-) ;-)



thanks but it was very simple ! i solved it !
now i can create a Sales Factor with this solution !!! :-) ;-)

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()) ;
}


这篇关于如何在C#中向Datagridview添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆