DataGridView and Search Query
I am making an windows application form for a resturant managment system which is connected with the access database(OLEDB) and i want to apply a query that is the selected value/name of the products in the combobox will show the result of the selected product in the datagridview box. I searched a lot from internet but cant get the proper code for this. Thanx in advance if anyone can help me.

Pravin GhadgeRank #1,340 · 1.3K reputationPosted Jan 1, 2012, 3:26 AM EST
double SumOfPrice=0;
for(int i=0;i
SumOfPrice=SumOfPrice+ Convert.ToDouble(ds.Tables[0].Rows[i]["columnname"].ToString());
}
Label1.Text=SumOfPrice.ToString(); //Dispaly sum in label.
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 30, 2011, 7:39 AM EST
Try this...
Run the attachments.
App.config
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 Datagridview_bind
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oda;
DataSet ds;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from product";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["prodname"]);
}
reader.Close();
con.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from product where prodname='" + comboBox1.Text.Trim() + "'";
com = new OleDbCommand(str, con);
oda = new OleDbDataAdapter(com);
ds = new DataSet();
oda.Fill(ds, "product");
dataGridView1.DataMember = "product";
dataGridView1.DataSource = ds;
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer
wasif iqbal9 reputationPosted Dec 30, 2011, 10:20 AM EST
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 30, 2011, 4:58 PM EST
In oda.fill(ds,"product") line, product is the table name.
Thanks
wasif iqbal9 reputationPosted Dec 31, 2011, 11:15 AM EST
Pravin GhadgeRank #1,340 · 1.3K reputationPosted Dec 31, 2011, 5:09 PM EST
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from product where prodname='" + comboBox1.Text.Trim() + "'";
com = new OleDbCommand(str, con);
oda = new OleDbDataAdapter(com);
ds = new DataSet();
oda.Fill(ds, "product");
dataGridView1.DataMember = "product";
dataGridView1.DataSource = ds;
double SumOfPrice=0;
for(int i=0;i
SumOfPrice=SumOfPrice+ds.Tables[0].Rows[i]["columnname"];
}
Label1.Text=SumOfPrice.ToString(); //Dispaly sum in label.
con.Close();
}
}
wasif iqbal9 reputationPosted Dec 31, 2011, 11:39 PM EST
Sam HobbsRank #107 · 20.5K reputationPosted Jan 1, 2012, 2:23 PM EST