Popular GridView with Access Data

0

I've been able to do this in VB.NET , but now I need to do this in C# and I can not turn the code correctly.

I have this GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="BatchID" >
    <Columns>
        <asp:BoundField DataField="BatchID" HeaderText="BatchID" InsertVisible="False" ReadOnly="True" SortExpression="BatchID" />
        <asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" />
        <asp:BoundField DataField="BatchSize" HeaderText="BatchSize" SortExpression="BatchSize" />
        <asp:BoundField DataField="Priority" HeaderText="Priority" SortExpression="Priority" />
        <asp:BoundField DataField="StartReq" HeaderText="StartReq" SortExpression="StartReq" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
    </Columns>
</asp:GridView>

The data I put into it, I bring through an Access database that I created. In VB.NET I was able to make it work, but I need to make this code work in C# now.

This is the code in VB.NET that I need to pass to C#

Private Sub GridView1_LoadData(sender As Object, e As EventArgs) Handles Me.Load
        Dim dtAllBatches As New Northwind.AllBatchesDataTable
        Using da As New NorthwindTableAdapters.AllBatchesTableAdapter
            da.Fill(dtAllBatches)
        End Using
            GridView1.DataSource = dtAllBatches.DefaultView
            GridView1.DataBind()
End Sub  

I was doing in VB.NET , but I was wrong and it was to be done in C# . I tried to find a way to make it work, on the internet, but I could not find something that would help me and at least understand how I should do it.

    
asked by anonymous 20.07.2017 / 19:37

1 answer

0

I hope it helps.

string strProvider = "@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\ArquivoAcess.accdb";
string strSql = "Select * from [Nome_Tabela]";
OleDbConnection con = new OleDbConnection(strProvider);
OleDbCommand cmd = new OleDbCommand(strSql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable tabelaTmp = new DataTable();
da.Fill(tabelaTmp);
dataGridView1.DataSource = tabelaTmp;
    
20.07.2017 / 20:20