Bbb
Bbb

Reputation: 649

Gridview, dynamic data within asp.net not working?

When I use the code below, my gridview (ID=Gridview2) object will populate perfectly using the datasource that is hard coded. I'd like to use a dynamically generated datatable (which when debugging the datatable is populated successfully complete with rows and columns). When I try to bind the dynamic data to my other gridview object (ID=Gridview1) and I verify that it has the datasource as my newly created datatable, nothing appears on the screen?

What am I doing wrong? Do I need to define templates for the dynamic code to work?

<asp:GridView ID="GridView2" runat="server" AllowSorting="True" BackColor="#DEBA84" 
    BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
    CellSpacing="2" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FFF1D4" />
    <SortedAscendingHeaderStyle BackColor="#B95C30" />
    <SortedDescendingCellStyle BackColor="#F1E5CE" />
    <SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="true"
    EmptyDataText="No results found" BackColor="#DEBA84" BorderColor="#DEBA84" 
    BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
    AllowSorting="True" AutoGenerateColumns="True">
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FFF1D4" />
    <SortedAscendingHeaderStyle BackColor="#B95C30" />
    <SortedDescendingCellStyle BackColor="#F1E5CE" />
    <SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>

Code-behind:

protected void btnSearch_Click(object sender, EventArgs e)
{       
    string domainToQueryFor = "domain";
    string pinToQueryFor = "account";
    DBConnectionlib.DBClass dbReader = new DBConnectionlib.DBClass();
    dbReader.connectionInformation = @"Server=tcp:XXXXXXXXX,1433 ;Database=" + databaseName + ";Trusted_Connection=false;UID=" + databaseUserName + ";Pwd=" + databasePassword + "; ApplicationIntent=ReadWrite;Timeout=60;MultiSubnetFailover=True";
    dbReader.tableName = tableName;
    string currentSqlQuery = "select * from " + dbReader.tableName + " WHERE domain like '" + domainToQueryFor + "' and pin like '" + pinToQueryFor + "'";
    dbReader.queryStatement = currentSqlQuery;
    List<string> results = dbReader.readFromSqlDatabaseReturnList();
    DataTable dt = createDataTable(results);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

Upvotes: 0

Views: 364

Answers (1)

Bbb
Bbb

Reputation: 649

I had to add this to get it to portray dynamically

foreach (System.Data.DataColumn item in dt.Columns)
            {
                BoundField nameColumn = new BoundField();
                nameColumn.DataField = item.ColumnName;
                nameColumn.HeaderText = item.ColumnName;
                GridView1.Columns.Add(nameColumn);
            }

Basically without the column names explicitly stated/added, the gridview will not display. They could probably also be hard coded in the html code with a <%bind%> command, but I wanted it more dynamic so I did it the above way.

Upvotes: 1

Related Questions