| |

VerySource

 Forgot password?
 Register
Search
View: 754|Reply: 5

DataGridView Total Item Issues

[Copy link]

2

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-3-20 09:30:01
| Show all posts |Read mode
The data is read from the database to the datatable and then added totals at the bottom of the window
string selectCommand = "select ID, GXID, GXname, GXprice from WS_GX where ProductID = 1";
dataAdapter = new OleDbDataAdapter (selectCommand, conn);
DataSet ds = new DataSet ();
dataAdapter.Fill (ds, "WS_GX");
DataTable table = ds.Tables ["WS_GX"];
object objSum = table.Compute ("Sum (GXprice)", null);
bindingSource1.DataSource = table;

In the above code, objSum is used to calculate the total

But whatever
selectCommand = "select ID, GXID, GXname, GXprice from WS_GX where ProductID = 1";
still is
selectCommand = "select ID, GXID, GXname, GXprice from WS_GX";


The value of the total item is added up according to all the items in the database, which causes the total error. How can I solve it ????
Reply

Use magic Report

2

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-6-25 14:00:01
| Show all posts
My code

// Get summary first
object objSum = newTable.Compute("Sum(number)", null );//newTable is the dataTable you requested and
                                                     //"Quantity" is the required field in your table
// Add new row
DataRow sum = newTable.NewRow();
sum[0] = "Total";
sum["Quantity"] = objSum.ToString();

newTable.Rows.Add(sum);
Reply

Use magic Report

1

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-24 06:45:01
| Show all posts
No new row?
bindingSource1.DataSource = table;?
//An improper function written by accident
private void SelData(string selquery,string tablename)
        {
            OleDbDataAdapter da = new OleDbDataAdapter(selquery, mc);
            DataSet ds = new DataSet();
            da.Fill(ds, tablename);
            DataTable dt = ds.Tables[tablename];
            DataRow dr = ds.Tables[tablename].NewRow();
            dataGridView1.DataSource = ds.Tables[tablename];
            object objSum = dt.Compute("Sum(GXprice)", null);
            string san = objSum.ToString();
            if (san=="")//When GXprice is empty.. If there is nothing empty, you can save this judgment!
            {
                objSum = (object)0;
            }
            dr[GXprice] = objSum.ToString();
            ds.Tables[tablename].Rows.Add(dr);
        }
Reply

Use magic Report

2

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-7-24 17:15:01
| Show all posts
Thanks, let me try
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-24 19:30:01
| Show all posts
object objSum = table.Compute("Sum(GXprice)", null);
You used "Sum()", which should use "Group by"

which is
object objSum = table.Compute("Sum(GXprice)", "Group by (except GXprice according to the field to be counted)");
Reply

Use magic Report

2

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-7-26 09:00:01
| Show all posts
understood
object objSum = table.Compute("Sum(GXprice)", "ProductID="+requestID);
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list