Click here to Skip to main content
16,013,322 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
string dbconnection = (@"Data Source=EMIII-PC;Initial Catalog=gymdatabase;Integrated Security=True");

public void BindMyData()
{
    SqlConnection conn = new SqlConnection(dbconnection);
    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM Student", conn);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        myDataGrid.ItemsSource = ds.Tables[0].DefaultView;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
    finally
    {
        conn.Close();
    }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    BindMyData();
}
Posted
Updated 5-Jul-14 4:17am
v2
Comments
CHill60 5-Jul-14 9:30am    
Edit this to put the full quesrion in the body of the post and use a sensible ritle

1 solution

few options :
you can select only the columns you want. for example
SQL
SELECT Col1, Col2 FROM Student

or set auto generate column false in your grid and add columns with binding properties for the columns you want
Read :
DataGridView.AutoGenerateColumns Property[^]
C#: Adding Columns To Bound DatagridView With Code[^]
 
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