Click here to Skip to main content
16,007,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I've two buttons which gets its records fron sql server when clicked (sp).
Both Buttons are fetching the datagridview corerectly.
The dgv columns are as ff (A,B,C and D). Column C is (A - B).
If i click btn1 it displays me the records perfectly BUT if i click btn2 and the later click btn1, it displays me the records as ff (A,B,D and C). But that's not what i'm expecting.
Is there a solution to make the records display as in the sp? (A,B,C,D)


C#
private void btn1_Click(object sender, EventArgs e)
            {
                if (cbBebene.SelectedItem != null)
                {
                    string C = ConfigurationManager.ConnectionStrings["S"].ConnectionString;
                    SqlConnection con = new SqlConnection(C);

        // TSQL Statement to choose tables dynamic
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = ("[dbo].[spBeFKZ]");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Table_Name", cbBebene.SelectedValue.ToString());
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);                

        try
        {
            con.Open();
            dt = new System.Data.DataTable();
            adapter.Fill(dt);
            dgvBerichte.AutoGenerateColumns = true;
            dgvBerichte.DataSource = dt;
                                                                              
        lbCount.Text = "Anzahl : " + dgvBerichte.Rows.Count.ToString();

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            con.Close();
                        }                
                }
                else
                {
                    MessageBox.Show("OnClick safety control!!! Periode is empty");
                }

            }


C#
private void btn2_Click(object sender, EventArgs e)
            {
                if (cbBebene.SelectedItem != null)
                {
                    //Connection string
                    string C = ConfigurationManager.ConnectionStrings["S"].ConnectionString;
                    SqlConnection con = new SqlConnection(C);

        // TSQL Statement to choose tables dynamic
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = ("[dbo].[spBeTp]");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Table_Name", cbBebene.SelectedValue.ToString());
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            try
            {
                con.Open();
                dt = new System.Data.DataTable();
                adapter.Fill(dt);
                dgvBerichte.AutoGenerateColumns = true;
                dgvBerichte.DataSource = dt;
                /* change the color of GWU Delta */
                dgvBerichte.Columns[3].DefaultCellStyle.ForeColor = Color.Blue;
                             
                lbCount.Text = "Anzahl : " + dgvBerichte.Rows.Count.ToString();

                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            con.Close();
                        }                   
                }
                else
                {
                    MessageBox.Show("OnClick safety control!!! Periode is empty");
                }

            }
Posted
Updated 27-Jan-15 3:37am
v5

Three solutions:
1) in the stored procedure change the order of the fields to fit ABCD.
2) don't allow AutoGenerateColumns, define your own order and bind by column name - BEST ONE, you have complete control...
3) depending on the SP you call, set columnindex property (worst and only included for completeness)

If this helps please take times to accept the solution. Thank you.
 
Share this answer
 
Comments
mikybrain1 27-Jan-15 10:31am    
Hi Sinisa
Can u plz give an example for point 2 excluding the AutoGenerateColumns?
Ignore the column names, these are in my native language, you have to put your own, obviously.

VB
With dgv
AutoGenerateColumns = False
                .Columns(0).DataPropertyName = "redni_br"
                .Columns(1).DataPropertyName = "id_zad"
                .Columns(2).DataPropertyName = "id"
                .Columns(3).DataPropertyName = "id_kutije"
                .Columns(4).DataPropertyName = "status"
                .Columns(5).DataPropertyName = "greska"
                .Columns(6).DataPropertyName = "razlog"
                .Columns(7).DataPropertyName = "tip"

                .DataSource = source.Tables(1)
            End With


In the grid designer you can find Columns property and set the columns, headers, alignement, null values, formats etc...then you just bind them in form load to particular column...you can use column names instead of indexes if you find it more readable.

You can bind the columns in form load and only assign datasource when it is available or you can bind them just before assigning the source. IT doesn't work other way around :)

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
v2

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