Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am making a Windows Form in C#.I have a DataGridView and a combobox, all I want to do is if i select a table in the combobox, it should display me the results in the datagridview.

I have done something like this: BUT i' m getting this error : 'System.Data.DataRowView' does not contain a definition
C#
private void cbTbl_SelectedIndexChanged(object sender, EventArgs e)
       {
           if (cbTbl.SelectedValue != null)
           {
               // Sql Server Connection and statement if a value is selected

               string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
               SqlConnection con = new SqlConnection(CS);
               SqlCommand sqlCmd = new SqlCommand();
               sqlCmd.Connection = con;
               sqlCmd.CommandType = CommandType.Text;
               sqlCmd.CommandText = ("SELECT * FROM " + cbTbl.SelectedValue); // Dynamically
               SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

               try
               {
                   con.Open();

                   dt = new DataTable();
                   sqlDataAdap.Fill(dt);
                   dataGridView1.AutoGenerateColumns = true;
                   dataGridView1.DataSource = dt;

               dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

                   con.Close();

               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           }


I need to cast the SelectedItem into DataRowView in order to get a value from this DataRowView But i don't know where and how.

I'm filling the combobox with this method:

C#
private void FillCombobox1()
{
// Sql Connection and statement
string C = (@"Data Source=");
SqlConnection con = new SqlConnection(C);


SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");
try
{

con.Open();

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();
adapter.Fill(dt);

//Fill combobox with data in DT
comboBox1.DataSource = dt;

//Display dbo Tables in cbAlt
comboBox1.DisplayMember = "TABLE_NAME";
comboBox1.ValueMember = "TABLE_NAME";

// Empty bzw. clear the combobox
comboBox1.SelectedIndex = -1;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
Posted
Updated 10-Nov-14 3:36am
v4
Comments
Herman<T>.Instance 10-Nov-14 7:33am    
In which line you get the exception?
mikybrain1 10-Nov-14 9:08am    
HiIt doesn't really indicate which line. It' a message box clue :)
Sinisa Hajnal 10-Nov-14 9:22am    
Set a breakpoint at the start of the method and step through the code.
Sinisa Hajnal 10-Nov-14 7:36am    
How are you filling your cbTbl? How are you setting its valueMember?
mikybrain1 10-Nov-14 9:04am    
I'm filling it with a self made method:
private void FillCombobox1()
{
// Sql Connection and statement
string C = (@"Data Source=");
SqlConnection con = new SqlConnection(C);


SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");
try
{

con.Open();

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();
adapter.Fill(dt);

//Fill combobox with data in DT
comboBox1.DataSource = dt;

//Display dbo Tables in cbAlt
comboBox1.DisplayMember = "TABLE_NAME";
comboBox1.ValueMember = "TABLE_NAME";

// Empty bzw. clear the combobox
comboBox1.SelectedIndex = -1;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
Can u help?

1 solution

Well,that's a common error. When you fill the combobox, set the DisplayMember and ValueMember before binding the data. Like this:

C#
//Display dbo Tables in cbAlt
comboBox1.DisplayMember = "TABLE_NAME";
comboBox1.ValueMember = "TABLE_NAME";
//Fill combobox with data in DT
comboBox1.DataSource = dt;


The problem you have is because when you bind the data with comboBox1.DataSource = dt, the comboBox1.SelectedIndexChanged is fired. But as the valueMember is still not set, what is passed to the event is a DataRowView,causing the error.

Edit

If you want to prevent to load the grid when you bind the data to the combo,do something like this:

C#
bool firstLoad=true;
.
.
.

private void cbTbl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!firstLoad)
            {
              if (cbTbl.SelectedValue != null)  
              {  
                // Sql Server Connection and statement if a value is selected

                string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                SqlConnection con = new SqlConnection(CS);
                SqlCommand sqlCmd = new SqlCommand();
                sqlCmd.Connection = con;
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = ("SELECT * FROM " + cbTbl.SelectedValue); // Dynamically
                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
 
                try 
                {
                    con.Open();
                   
                    dt = new DataTable();
                    sqlDataAdap.Fill(dt);
                    dataGridView1.AutoGenerateColumns = true;
                    dataGridView1.DataSource = dt;
                    
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                    
                    con.Close();
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
             }
           }
           else
           {
             firstLoad=false;
           }
       }


That is,use a boolean variable to prevent loading the datagrid view when the combobox data is binded.
 
Share this answer
 
v2
Comments
mikybrain1 10-Nov-14 10:21am    
Thnx Pikoh.
But it now fills the dgv automatically without choosing a table.
I wanna first choose a table b4 it loads the dgv
Pikoh 10-Nov-14 11:10am    
See updated solution
Sinisa Hajnal 11-Nov-14 1:56am    
You could add empty row at the top of the selection, setting table name value empty string and table name property something like "Choose a table..." then you simply ignore first line (SelectedValue = "" or SelectedIndex = 0)
Pikoh 11-Nov-14 2:58am    
That's even better :)
mikybrain1 11-Nov-14 5:39am    
Thnx very much. It was a great help and i wish u guys a nice day.
@Pikoh & Sinisa

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