0
Public Class Recipients
Private Sub Recipients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    mycom.Connection = cn
    mycom.CommandText = "Select Idno,Name,Course,YearSec,Organization from tbl_students"
    myr = mycom.ExecuteReader

    While myr.Read
        With grdRecipients
            .Rows.Add()
            .Rows(.RowCount - 1).Cells(0).Value = myr(0).ToString
            .Rows(.RowCount - 1).Cells(1).Value = myr(1).ToString
            .Rows(.RowCount - 1).Cells(2).Value = myr(2).ToString
            .Rows(.RowCount - 1).Cells(3).Value = myr(3).ToString
            .Rows(.RowCount - 1).Cells(4).Value = myr(4).ToString
        End With
    End While
    myr.Close()
End Sub
End Class

I have a grid view called grdRecipients which loads the data in my database using the select statement. Column names are manually inputted in the data grid view. Any help with automatically creating their own column names and adding a checkbox column.

Any help is apprecited. . . thanks

1 Answer 1

1

To automatically create the column names, you can create a DataTable based on the SQL query and set it as a source to the DataGridView. To have a CheckBox column, there must be a column in the DataTable with a Boolean data type. It can be either manually created or it can be retrieved from the query.

Check the following code for more information

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    mycom.Connection = cn
    mycom.CommandText =
        <SQL>
            SELECT
                Idno
                ,Name
                ,Course
                ,YearSec
                ,Organization
            FROM tbl_students
        </SQL>.Value

    If cn.State = ConnectionState.Closed Then
        cn.Open()
    End If

    Dim myadap As New SqlDataAdapter(mycom)
    Dim mydt As New DataTable
    myadap.Fill(mydt)

    mydt.Columns.Add("CheckBoxColumn", GetType(Boolean))
    grdRecipients.DataSource = mydt

    myadap.Dispose()
End Sub
Sign up to request clarification or add additional context in comments.

8 Comments

Value of type 'MySql.Data.MySqlClient.MySqlCommand' cannot be converted to 'System.Data.SqlClient.SqlCommand' I get this error on the line: Dim myadap As New SqlDataAdapter(mycom)
Replace the line Dim myadap As New SqlDataAdapter(mycom) to Dim myadap As New MySqlDataAdapter(mycom)
@ David, thanks great help, your comment satisfied my problem, any knowledge on how to get the values selected?
If I understand you correctly, you want to check the checkboxes right? to check a checkbox, you can set the value of the corresponding row for the CheckBoxColumn to true. For example if you want to check the first row's checkbox, you can use this line mydt.rows(0).item("CheckBoxColumn") = true. The 0 represent the index of the first line. The row must exist before you can check its checkbox.
what i mean is, from the code you gave me, except the mydt.rows(0).item("CheckBoxColumn") = true, how can i allow the checkboxes to be checked and how can i get the values of the checked rows and pass it to the other form? thanks in advance
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.