如何在每个按钮点击C sharp winforms时添加到gridcontrol一个新行 [英] How to add to gridcontrol a new row at every button click in C sharp winforms

查看:98
本文介绍了如何在每个按钮点击C sharp winforms时添加到gridcontrol一个新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个按钮上添加 行到gridcontrol单击。我尝试了很多方法但没有成功。我正在发送我的代码。 
有没有人帮我 out 以上?感谢 advance 您的宝贵回复。 ?





我的尝试:



  private   void  B_Click( object  sender,EventArgs e)
{
Button bt =(Button)sender;
int productId =( int )bt.Tag;
AddProductDataContext db = new AddProductDataContext();
十进制数量;
decimal .TryParse(txtCalculator.Text, out 数量);
var results = 来自 inv db.Inventories
其中 inv.RecId == productId
选择 < span class =code-keyword> new
{
inventoryName = inv.InventoryName,
数量,
总计=数量* inv.InventoryPrice
};

DataTable dt = new DataTable();
dt.Columns.Add( inventoryName);
dt.Columns.Add( Quantity);
dt.Columns.Add( Total);

foreach var x 结果中)
{
DataRow newRow = dt.Rows.Add();
newRow.SetField( inventoryName,x.inventoryName);
newRow.SetField( Quantity,x .Quantity);

newRow.SetField( Total,x.Total);

}

gridControl1.DataSource = dt;
gridView1.AddNewRow();
}

解决方案

很抱歉,我误解了你的问题。如果要在按钮单击时向DataTable对象添加新行,则必须在按钮单击事件之外定义DataTable对象。否则每次只添加一行。所以请尝试如下。



使用私有变量来定义像这样的DataTable对象



< pre lang =C#> private DataTable dt;





然后初始化Form加载事件中的DataTable对象如:



 dt =  new 数据表(); 
dt.Columns.Add( inventoryName);
dt.Columns.Add( Quantity);
dt.Columns.Add( Total);





然后使用您之前的代码如下:



  private   void  B_Click( object  sender,EventArgs e) 
{
Button bt =(Button)sender;
int productId =( int )bt.Tag;
AddProductDataContext db = new AddProductDataContext();
十进制数量;
decimal .TryParse(txtCalculator.Text, out 数量);
var results = 来自 inv db.Inventories
其中 inv.RecId == productId
选择 < span class =code-keyword> new
{
inventoryName = inv.InventoryName,
数量,
总计=数量* inv.InventoryPrice
};

foreach var x 结果中)
{
DataRow newRow = dt.NewRow();
newRow.SetField( inventoryName,x.inventoryName);
newRow.SetField( Quantity,x .Quantity);

newRow.SetField( Total,x.Total);
dt.Rows.Add(newRow);
}

gridControl1.DataSource = dt;

}





请告诉我。


这是解决方案:



 DataTable dt =  new  DataTable(); 
private void B_Click( object sender,EventArgs e)
{

Button bt =(Button)sender;
int productId =( int )bt.Tag;
AddProductDataContext db = new AddProductDataContext();
十进制数量;
decimal .TryParse(txtCalculator.Text, out 数量);
var results = 来自 inv db.Inventories
其中 inv.RecId == productId
选择 < span class =code-keyword> new
{
inventoryName = inv.InventoryName,
数量,
总计=数量* inv.InventoryPrice
};

foreach var x 结果中)
{
DataRow newRow = dt.NewRow();
newRow.SetField( inventoryName,x.inventoryName);
newRow.SetField( Quantity,x .Quantity);

newRow.SetField( Total,x.Total);
dt.Rows.Add(newRow);



}


gridControl1.DataSource = dt;


}
私人 void SaleScreen_Load(< span class =code-keyword> object sender,EventArgs e)
{

dt.Columns.Add( inventoryName);
dt.Columns.Add( Quantity);
dt.Columns.Add( Total);

}





我用这种方式解决了这个问题。感谢大家的宝贵帮助。


I want to add new row to gridcontrol at every button click. I tried many ways but no success. I am sending my code.
Is there anyone to help me out there for the above? Thanks in advance for your precious replies. 



What I have tried:

private void B_Click(object sender, EventArgs e)
        {
   Button bt = (Button)sender;
       int productId = (int)bt.Tag;
       AddProductDataContext db = new AddProductDataContext();
       decimal Quantity;
       decimal.TryParse(txtCalculator.Text, out Quantity);
     var results  = from inv in db.Inventories
                                          where inv.RecId == productId
                                          select new
                                          {
                                              inventoryName = inv.InventoryName,
                                              Quantity,
                                              Total = Quantity * inv.InventoryPrice
                                          };

               DataTable dt = new DataTable();
               dt.Columns.Add("inventoryName");
               dt.Columns.Add("Quantity");
               dt.Columns.Add("Total");

               foreach (var x in results)
               {
                   DataRow newRow = dt.Rows.Add();
                   newRow.SetField("inventoryName", x.inventoryName);
                   newRow.SetField("Quantity", x.Quantity);

                   newRow.SetField("Total", x.Total);

               }

               gridControl1.DataSource = dt;
               gridView1.AddNewRow();
}

解决方案

Sorry as I misunderstood your problem. If you want to add new row to the DataTable object on button click, then you have to define the DataTable object outside the button click event. Otherwise each time it will add only one row only. So please try as below.

Use a private variable to define the DataTable object like this

private DataTable dt;



Then initialize the DataTable object in Form load event like:

dt = new DataTable();
dt.Columns.Add("inventoryName");
dt.Columns.Add("Quantity");
dt.Columns.Add("Total");



Then use your previous code as below:

private void B_Click(object sender, EventArgs e)
        {
   Button bt = (Button)sender;
       int productId = (int)bt.Tag;
       AddProductDataContext db = new AddProductDataContext();
       decimal Quantity;
       decimal.TryParse(txtCalculator.Text, out Quantity);
     var results  = from inv in db.Inventories
                                          where inv.RecId == productId
                                          select new
                                          {
                                              inventoryName = inv.InventoryName,
                                              Quantity,
                                              Total = Quantity * inv.InventoryPrice
                                          };
 
               foreach (var x in results)
               {
                   DataRow newRow = dt.NewRow();
                   newRow.SetField("inventoryName", x.inventoryName);
                   newRow.SetField("Quantity", x.Quantity);
 
                   newRow.SetField("Total", x.Total);
                   dt.Rows.Add(newRow);
               }
 
               gridControl1.DataSource = dt;
               
}



Please let me know.


Here is the solution:

DataTable dt = new DataTable();
    private void B_Click(object sender, EventArgs e)
    {

        Button bt = (Button)sender;
        int productId = (int)bt.Tag;
        AddProductDataContext db = new AddProductDataContext();
        decimal Quantity;
        decimal.TryParse(txtCalculator.Text, out Quantity);
        var results = from inv in db.Inventories
                      where inv.RecId == productId
                      select new
                      {
                          inventoryName = inv.InventoryName,
                          Quantity,
                          Total = Quantity * inv.InventoryPrice
                      };

        foreach (var x in results)
        {
            DataRow newRow = dt.NewRow();
            newRow.SetField("inventoryName", x.inventoryName);
            newRow.SetField("Quantity", x.Quantity);

            newRow.SetField("Total", x.Total);
            dt.Rows.Add(newRow);



        }


        gridControl1.DataSource = dt;


    }
    private void SaleScreen_Load(object sender, EventArgs e)
    {

        dt.Columns.Add("inventoryName");
        dt.Columns.Add("Quantity");
        dt.Columns.Add("Total");

    }



I solved the problem in this way. Thanks to everybody for their precious helps.


这篇关于如何在每个按钮点击C sharp winforms时添加到gridcontrol一个新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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