I have table datagridview

i want count people in grade
and when cilck button count is show messagebox " A="3" B="1" C"1" "
how to ??
I can't do it. help me please :(
thank

i want count people in grade
and when cilck button count is show messagebox " A="3" B="1" C"1" "
how to ??
I can't do it. help me please :(
thank

SenthilkumarRank #387 · 6.5K reputationPosted Apr 14, 2012, 5:46 AM EDT
You can use the following LINQ to group by count.
if you can use LINQ, then the groupby clause will do the aggregation for you.
I have tested and this is working fine as you expected.
Biw Mhtm9 reputationPosted Apr 14, 2012, 6:08 AM EDT
If i don't insert new data.
how to connect datagridview?
(I use ADO.net oleDb)
I never use linq, but i will try
SenthilkumarRank #387 · 6.5K reputationPosted Apr 14, 2012, 6:23 AM EDT
If you read from the database then definitely you will be having DataSet. Then you can use the DataSet1.Table[0];
Usually if you do not have any data in the table and you are trying to bind the result..
SELECT ID, Grade FROM Students
It may not have any record in the sql server , still you can bind to the DataGridView. It will have the empty data grid.
Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 14, 2012, 9:55 AM EDT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Grade_Count
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
string str;
DataSet ds;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from test";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "test");
dataGridView1.DataMember = "test";
dataGridView1.DataSource = ds;
con.Close();
}
private void btn_count_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "SELECT COUNT(id) AS Total, grade FROM test GROUP BY grade";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "test");
dataGridView2.DataMember = "test";
dataGridView2.DataSource = ds;
con.Close();
}
}
}
Thanks
Biw Mhtm9 reputationPosted Apr 14, 2012, 7:18 PM EDT