简体   繁体   中英

"No row can be added to a DataGridView control that does not have columns. Columns must be added first." in VB.net

I have been encountering an error in my VB.Net code that says "No row can be added to a DataGridView control that does not have columns. Columns must be added first.". I was trying to code a live search on 3 datagrids and only 2 of them are working even though they have the same code. Please help me.

Here is the code for the datagrid live search that encounters an error:

Private Sub txtProdSearch_TextChanged(sender As Object, e As EventArgs) Handles txtProdSearch.TextChanged

    Connect()
    Query = "select * from tbl_productdetails where name like '%' @name '%' or code like '%' @code '%'"

    With Command
        .CommandText = Query
        .Connection = Conn
        .Parameters.Clear()
        .Parameters.Add("@name", MySqlDbType.VarChar).Value = txtProdSearch.Text.Trim
        .Parameters.Add("@code", MySqlDbType.VarChar).Value = txtProdSearch.Text.Trim

        Reader = .ExecuteReader
    End With

    dgProd.Rows.Clear()

    While Reader.Read
        dgProd.Rows.Add(Reader("code"), Reader("name"), Reader(2), Reader(3), Reader(4))
    End While
End Sub

And here is a code of one of the working datagrid live search:

Private Sub txtSearchTrans_TextChanged(sender As Object, e As EventArgs) Handles txtSearchTrans.TextChanged
    Connect()
    Query = "select * from tbl_sales where transaction_num like '%' @transaction_num '%'"

    With Command
        .CommandText = Query
        .Connection = Conn
        .Parameters.Clear()
        .Parameters.Add("@transaction_num", MySqlDbType.VarChar).Value = txtSearchTrans.Text.Trim

        Reader = .ExecuteReader
    End With

    dgSales.Rows.Clear()

    While Reader.Read
        dgSales.Rows.Add(Reader("transaction_num"), Reader(1), Reader(2))
    End While
End Sub

Here's how I've been approaching GridViews. First the HTML:

<asp:GridView CssClass="gv" ID="gv" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" HorizontalAlign="Center"
HeaderStyle-CssClass="FixedHeader" DataKeyNames="ThingID" OnRowDataBound="gv_RowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
    <asp:ButtonField ButtonType="Button" CommandName="Select" Text="View Thing Details" 
        HeaderStyle-Width="120px" ItemStyle-Width="120px" ControlStyle-Width="120px">
    </asp:ButtonField>
    <asp:TemplateField HeaderText="Thing ID"
        HeaderStyle-Width="75px" ItemStyle-Width="75px">
        <ItemTemplate>
            <asp:Label ID="lblThingID" runat="server" Text='<%#Eval("ThingID")%>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

You can add as many template fields as you want and style however you like. Then on the vb side:

Imports System.Data.SqlClient

Private Sub gvLoad()

    Dim con as new SqlConnection(YourConString)
    Dim cmd As SqlCommand
    Dim da As SqlDataAdapter
    Dim dt As DataTable
    Dim RecordCount As Integer = 0

    Dim Query as string ="SELECT ThingID FROM tbl_things"
    con.open()
    cmd = new SqlCommand(Query,con)
    dt = new DataTable
    da = new SqlDataAdapter(cmd)
    RecordCount = da.fill(dt)
    con.close()
    
    gv.DataSource = dt.DefaultView
    gv.DataBind()

End Sub

You can do a lot more to 'pretty it up' and a Try/Catch is always a good idea but this is the basic idea: Create column templates for your gv with HTML and then load the whole thing up with a DataTable.

Hopefully that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM