Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
C#
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:

C#
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();
}
Posted
Updated 14-May-16 1:04am
Comments
Debasis.logica 13-May-16 7:45am    
Hi,

Please alter the foreach section like below and check (also I think you can remove the last line i.e. gridView1.AddNewRow();):

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);
}
Yucel Emir 14-May-16 2:58am    
I tried but same result, the new row is not initialized when I clicked the button for second time.
Richard Deeming 13-May-16 9:38am    
REPOST
You have already posted this question:
http://www.codeproject.com/Questions/1099873/How-to-add-to-devexpress-gridcontrol-a-new-row-at[^]

If you want to update your question, go to the existing question and click "Improve question". Do not post your update as a new question!

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

C#
private DataTable dt;


Then initialize the DataTable object in Form load event like:

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


Then use your previous code as below:

C#
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.
 
Share this answer
 
Here is the solution:

C#
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.
 
Share this answer
 
v2

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