Hey Guys,

I have a question...

I want to display data from two different tables into a single datagrid view.
Problem is i dnt wana join the tables. pls check this code

private void bindgrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str1 = "Select * from wcl_et ";
str2 = "Select * from secl_et";
str3 = "select * from sccl_et";
ad = new SqlDataAdapter(str1, con);
ad.Fill(ds1, "wcl_et");
ad = new SqlDataAdapter(str2, con);
ad.Fill(ds2, "secl_et");
//ds1.Merge(ds2);
dataGridView1.DataSource = ds1.Tables[0].DefaultView;
//dataGridView1.DataSource = ds1;
//dataGridView1.DataBindings();
}

private void Form1_Load(object sender, EventArgs e)
{
bindgrid();
}

private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connStr);
ad = new SqlDataAdapter(str2, conn);
ad.Fill(ds2, "secl_et");
dataGridView1.DataSource = ds2.Tables[0].DefaultView;
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
SqlConnection conn1 = new SqlConnection(connStr);
ad = new SqlDataAdapter(str3, conn1);
ad.Fill(ds3, "sccl_et");
dataGridView1.DataSource = ds3.Tables[0].DefaultView;
}

the problem is...i want to display data of second table on the click of the button and i have written code for same. But if run this code it u wud find tht everytime u click button, it shows repeated records. Please help me with this....

Please, can you clarify what do you mean with 'repeated records'?
a) Allways show the same records at every refresh of the datagrid
b) All the rows shown have the same values
c) At every refresh, the data grid increases the number of rows, and the content of the new rows is exactly the same of the old rows

Thanks to clarify

Wrong post.

@LOLAFUERTES....


hey thanx for your reply...
yea..i m facing exact prblms tht u hv stated..

a) Allways show the same records at every refresh of the datagrid
b) All the rows shown have the same values
c) At every refresh, the data grid increases the number of rows, and the content of the new rows is exactly the same of the old rows


thanx

IMO, before ad.Fill(ds3, "sccl_et"); you need to empty the table with ds3.Tabres[0].Clear; Hope this helps

@lolafuertes...

hey thankz a lot for your help...


i need to ask one more question....

private void bbn_Click(object sender, EventArgs e)
        {
            OleDbDataAdapter ad;
            DataSet ds1 = new DataSet();
            
            if (comboBox1.SelectedIndex == 0)
            {
                string str1 = "select * from '"+comboBox1.SelectedIndex+"'";
                OleDbConnection conn1 = new OleDbConnection(str);
                ad = new OleDbDataAdapter(str1, conn1);

                ad.Fill(ds1);
                dataGridView1.DataSource = ds1.Tables[0].DefaultView;

i want the button to select the data from combo box and then display the corresponding table in grid view...

however its giving me an error

ad.Fill(ds1)

here...

it says syntax error..i know i need to write table name there...but i want it to read it from my

combobox.seleted index

....

Any suggestions....


thanks and regards

you "repeated rows show" problem will solved form this code...you shoud have to use the Is not post back function, if you want to not fire every time a biding grid view code.

private void Form1_Load(object sender, EventArgs e)

{
   if (IsPostBack == false)
        {
         bindgrid();
        }  

}

Regards,
jay

commented: This is not a web-app. -2

Instead of selected index, you need to use the selected item, and in the from clause of the select statement, you should remove the apostrophes.

Ie:

var tableName = comboBox1.SelectedItem as string;
string str1 = "select * from " + tablename;

and in the ad.Fill(ds1) the can try

ad.Fill(ds1, tableName)

Hope this helps

@Lolafuertes and JAY...

Thank you guys for your help..

lola especially you..

regards

ajinkya ragalwar

I am glad to help.

I this solved your problem, please be so kind to mark this thread as solved.
Thanks in advance.

ds3.Tabres[0].Clear;

working fine !!!!!!!

thanks
Lolafuertes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.