0

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();
}
3
  • Fwiw, I see you have an edit button on Grid1. If you plan on doing editing in that grid I strongly recommend using a datasource control that automatically creates CRUD methods. Manually/dynamically working with gridviews can be painful. And, you said the table has data but to be sure, I take it that tableName is defined somewhere - dbReader.tableName = tableName;? In the sql query you just use dbReader.tableName. Commented Jul 12, 2018 at 21:32
  • Yes, datatable and the database name, etc are defined globally. I can't be the only one who wants to query the database and not query/display * can I? Do I need to create a template for it to display? Commented Jul 13, 2018 at 11:55
  • I think your answer is the best bet. IIRC, the (only?) other way I've done it is to populate the grid with a data source control, which adds the fields, then delete the ds control. Not ideal. Commented Jul 13, 2018 at 22:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.