【发布时间】:2018-12-21 02:16:57
【问题描述】:
当我使用下面的代码时,我的 gridview (ID=Gridview2) 对象将使用硬编码的数据源完美填充。我想使用动态生成的数据表(在调试数据表时成功填充行和列)。当我尝试将动态数据绑定到我的其他 gridview 对象 (ID=Gridview1) 并验证它是否将数据源作为我新创建的数据表时,屏幕上什么也没有出现?
我做错了什么?我需要为动态代码定义模板吗?
<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>
代码隐藏:
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();
}
【问题讨论】:
-
Fwiw,我看到您在 Grid1 上有一个编辑按钮。如果您打算在该网格中进行编辑,我强烈建议您使用自动创建 CRUD 方法的数据源控件。手动/动态地使用网格视图可能会很痛苦。而且,您说该表有数据,但可以肯定的是,我认为
tableName是在某处定义的 -dbReader.tableName = tableName;?在 sql 查询中,您只需使用dbReader.tableName。 -
是的,数据表和数据库名称等都是全局定义的。我不能是唯一一个想要查询数据库而不是查询/显示的人 * 我可以吗?我需要创建一个模板才能显示吗?
-
我认为您的回答是最好的选择。 IIRC,(唯一?)我所做的其他方式是使用数据源控件填充网格,该控件添加字段,然后删除 ds 控件。不理想。
标签: asp.net gridview datatable aspxgridview