Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
now i am developing one windows form appplication by using ADO.Net concept .

Here i had created two comboboxes,one datagidview .
In the First Combobox displays Alla available tables in a database in the connection string and
in the second combobox it displays department numbers if first combobox select Emp table .

Now if user select Emp from 1st combobox then in second combobox it displays all department numbers.

Now if the user selects any deptno then all emplyoees which r under that selected department number must bew displayed in the datagrid view
But problem is that when user selects table from first combobox list all the employees r displayed in the datagrid view but when user selects deptno in the second combobox it doesnot displays ciressponding employees details Give me the solution for this plroblem.
Posted
Comments
nagendrathecoder 9-Aug-10 7:09am    
Can you show us some code snippet, its difficult to make out what is missing.
sKumarnvs 9-Aug-10 7:58am    
Hai nagendra ok Then i am sending the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AllTables
{
public partial class GetAll : Form
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader read;
SqlDataAdapter da,da2;
DataSet ds,ds1;
DataRow row;
DataView dv;
bool flag = false;
public GetAll()
{
InitializeComponent();
}

private void GetAll_Load(object sender, EventArgs e)
{
try
{
//estabilishing Connection
conn = new SqlConnection("Data Source=(local);Initial Catalog=Sateesh;Integrated Security=SSPI");
conn.Open();
comm = new SqlCommand("select name from sysobjects where xtype='u'", conn);
read = comm.ExecuteReader();
//Adding All available atbles To combobox1
while (read.Read())
cmbTab.Items.Add(read[0].ToString());
}
catch (Exception exec)
{
MessageBox.Show(exec.Message);
}
finally
{
if (conn != null)
conn.Close();
}

}

private void cmbTabSelect_SelectedIndexChanged(object sender, EventArgs e)
{

//Displaying all records Of table which is Selected from combobox1 in the gridview
da = new SqlDataAdapter("Select * from " + cmbTab.SelectedItem.ToString(), conn);
ds = new DataSet();
da.Fill(ds, cmbTab.SelectedItem.ToString());

dgvTable.DataSource = ds.Tables[cmbTab.SelectedItem.ToString()];

//int i=0;
//while (i < ds1.Tables[0].Rows.Count)
da.SelectCommand.CommandText = "Select Deptno from Dept";
da.Fill(ds, "Dept");
cmbFind.DataSource = ds.Tables["Dept"];
cmbFind.DisplayMember = "Deptno";


flag = true;
}

private void btnSave_Click(object sender, EventArgs e)
{
if (ds.HasChanges())
{
da.Update(ds, cmbTab.SelectedItem.ToString());
MessageBox.Show("Updations R Saved;");
}
else
MessageBox.Show("There R No Updations For Saving;");
}

private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}

private void cmbFind_SelectedIndexChanged(object sender, EventArgs e)
{
if (flag)
{
//here the place reqired code for when selecting deptno from combobox2 it has filter and display records in the gridview can u send code for this problem

}

}

}
}

Hi,

Here dgv -->DataGridView

// Get the Value from DataGridView to DataTable
DataTable dtData=(DataTable)dgv.DataSource;

// Filter Particular Id Value
string sEmployeeId=5;
string sCondition= "EmpId ='"+sEmployeeId+"";
DataRow[] drData=dtData.Select(sCondition);

//Set the Value into another DataTable
DatatTable dtOutput=new DataTable();
dtOutput=dtData.Clone();
foreach(DataRow dr in drData)
{
dtOutput.ImportRow(dr);
}

dgv.DataSource=dtOutput;

I think its work.If any queries ask to me.
Cheers... ;)
 
Share this answer
 
Comments
sKumarnvs 9-Aug-10 7:52am    
ya i try it but it returns an error like this
Here ui use Dno for U r SEmployeeId

string Dno = cmbFind.SelectedItem.ToString();

Object reference not set to an instance of an object.
sKumarnvs 9-Aug-10 8:01am    
Hai saravanann ok Then i am sending the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AllTables
{
public partial class GetAll : Form
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader read;
SqlDataAdapter da,da2;
DataSet ds,ds1;
DataRow row;
DataView dv;
bool flag = false;
public GetAll()
{
InitializeComponent();
}

private void GetAll_Load(object sender, EventArgs e)
{
try
{
//estabilishing Connection
conn = new SqlConnection("Data Source=(local);Initial Catalog=Sateesh;Integrated Security=SSPI");
conn.Open();
comm = new SqlCommand("select name from sysobjects where xtype='u'", conn);
read = comm.ExecuteReader();
//Adding All available atbles To combobox1
while (read.Read())
cmbTab.Items.Add(read[0].ToString());
}
catch (Exception exec)
{
MessageBox.Show(exec.Message);
}
finally
{
if (conn != null)
conn.Close();
}

}

private void cmbTabSelect_SelectedIndexChanged(object sender, EventArgs e)
{

//Displaying all records Of table which is Selected from combobox1 in the gridview
da = new SqlDataAdapter("Select * from " + cmbTab.SelectedItem.ToString(), conn);
ds = new DataSet();
da.Fill(ds, cmbTab.SelectedItem.ToString());

dgvTable.DataSource = ds.Tables[cmbTab.SelectedItem.ToString()];

//int i=0;
//while (i < ds1.Tables[0].Rows.Count)
da.SelectCommand.CommandText = "Select Deptno from Dept";
da.Fill(ds, "Dept");
cmbFind.DataSource = ds.Tables["Dept"];
cmbFind.DisplayMember = "Deptno";


flag = true;
}

private void btnSave_Click(object sender, EventArgs e)
{
if (ds.HasChanges())
{
da.Update(ds, cmbTab.SelectedItem.ToString());
MessageBox.Show("Updations R Saved;");
}
else
MessageBox.Show("There R No Updations For Saving;");
}

private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}

private void cmbFind_SelectedIndexChanged(object sender, EventArgs e)
{
if (flag)
{
//here the place reqired code for when selecting deptno from combobox2 it has filter and display records in the gridview can u send code for this problem
ds.Clear();
//dgvFilter.DataSource = ds.Tables["EmpIpFour"].Select("Deptno="+cmbFind.SelectedItem)[5];
DataTable dtData = (DataTable)dgvTable.DataSource;

// Filter Particular Id Value
string Dno = cmbFind.SelectedItem.ToString();
string sCondition = "Deptno =" + Dno ;
DataRow[] drData = dtData.Select(sCondition);

//Set the Value into another DataTable
DataTable dtOutput = new DataTable();
dtOutput = dtData.Clone();
foreach (DataRow dr in drData)
{
dtOutput.ImportRow(dr);
}

dgvTable.DataSource = dtOutput;

}

}

}
}
Hi sKumarn

I think don't clear the dataset value.Because its only fill into datagridview.

//Just Check the Condition
if(dgv.DataSource!=null)
{

// Get the Value from DataGridView to DataTable
DataTable dtData=(DataTable)dgv.DataSource;
.
.
.
.
.
}

Thanks,
Now u check it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900