Load table data to the Data Grid

3 posts, 0 answers
  1. Mr
    Mr avatar
    5 posts
    Member since:
    Sep 2012

    Posted 08 Dec 2013 Link to this post

    hi ..


    Previously I used following code to fill the Data Grid Microsoft's own
    But it does not work with the new tool Grid View


    myconn = New SqlConnection(constr)
           myconn.Open()
           mycomm = New SqlCommand("Select * from tab1 ", myconn)
           reader = mycomm.ExecuteReader
           reader.Read()
           DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value = reader(0).ToString.ToUpper
           DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = reader(1).ToString.ToUpper
           DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value = reader(2).ToString.ToUpper
           DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value = reader(3).ToString.ToUpper
           DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(4).Value = reader(4).ToString.ToUpper
           reader.Close()
           myconn.Close()


    What is the solution؟!
    THX




  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3948 posts

    Posted 11 Dec 2013 Link to this post

    Hello,

    Thank you for contacting Telerik Support.

    You can populate our RadGridView using the following code snippet:
    public Form1()
    {
        InitializeComponent();
     
        for (int i = 0; i < 18; i++)
        {
            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn("Col" + i);
            radGridView1.MasterTemplate.Columns.Add(textBoxColumn);
        }
     
        using (SqlConnection connection = new SqlConnection("connection string"))
        {
            connection.Open();
            string queryString = @"Select * from Employees";
            SqlCommand command = new SqlCommand(queryString, connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                GridViewRowInfo row = radGridView1.Rows.NewRow();
                for (int i = 0; i < radGridView1.Columns.Count; i++)
                {
                    row.Cells[i].Value = reader[i];
                }
                radGridView1.Rows.Add(row);
            }
            reader.Close();
        }
    }

    However, our Tutorial: Binding to DataTable or DataSet help article is quite useful about this.

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. Mr
    Mr avatar
    5 posts
    Member since:
    Sep 2012

    Posted 11 Dec 2013 Link to this post

    Thank you
Back to Top