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
Biw MhtmPosted Apr 14, 2012, 11:18 PM
Satyapriya NayakPosted Apr 14, 2012, 1:55 PM
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
SenthilkumarPosted Apr 14, 2012, 10:23 AM
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.
Biw MhtmPosted Apr 14, 2012, 10:08 AM
If i don't insert new data.
how to connect datagridview?
(I use ADO.net oleDb)
I never use linq, but i will try
SenthilkumarPosted Apr 14, 2012, 9:46 AM
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.