This is a migrated thread and some comments may be shown as answers.

Load table data to the Data Grid

2 Answers 67 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mr
Top achievements
Rank 1
Mr asked on 08 Dec 2013, 10:07 PM
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 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 11 Dec 2013, 04:05 PM
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 >>
0
Mr
Top achievements
Rank 1
answered on 11 Dec 2013, 09:52 PM
Thank you
Tags
GridView
Asked by
Mr
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Mr
Top achievements
Rank 1
Share this question
or