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 GhadgePosted Jan 1, 2012, 8:26 AM
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.
Sam HobbsPosted Jan 1, 2012, 7:23 PM
wasif iqbalPosted Jan 1, 2012, 4:39 AM
Pravin GhadgePosted Dec 31, 2011, 10:09 PM
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 iqbalPosted Dec 31, 2011, 4:15 PM
Satyapriya NayakPosted Dec 30, 2011, 9:58 PM
In oda.fill(ds,"product") line, product is the table name.
Thanks
wasif iqbalPosted Dec 30, 2011, 3:20 PM
Satyapriya NayakPosted Dec 30, 2011, 12:39 PM
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