我正在创建一个网站,我希望显示一个已切换行和列的数据表。我写了一些c#来做这件事。我的代码编译并运行而不会崩溃,但我放置此数据的gridview永远不会出现。如果我删除了将数据绑定到gridview的代码,而是使用sql源连接,则显示(但显然没有翻转)。
为什么我的gridview没有出现,我将如何修复它?
C#:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MachineUpdateFrontEnd
{
public partial class NewUpdate : System.Web.UI.Page
{
DataSet input;
DataSet output;
protected void Page_Load(object sender, EventArgs e)
{
DataSet temp = pullData(UpdateSqlDataSource.ConnectionString);
input = SwitchRows(temp);
GridView1.DataSource = input.Tables[0];
}
private DataSet pullData(string ConStr) {
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select * from MachineUpdate", ConStr);
adp.Fill(ds, "MachineUpdate");
return ds;
}
private void pushData() {
output = SwitchRows(input);
}
private DataSet SwitchRows(DataSet input)
{
DataSet ds = new DataSet();
foreach (DataTable dt in input.Tables) {
DataTable inputTable = new DataTable();
for (int i = 0; i < dt.Rows.Count; i++) {
inputTable.Columns.Add(Convert.ToString(i));
}
DataRow r;
for (int j = 0; j < dt.Columns.Count; j++) {
r = inputTable.NewRow();
r[0] = dt.Columns[j].ToString();
for (int k = 1; k < dt.Rows.Count; k++) {
r[k] = dt.Rows[k - 1][j];
}
inputTable.Rows.Add(r);
}
ds.Tables.Add(inputTable);
}
return ds;
}
}
}
ASP:
<%@ Page Title="" Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true" CodeBehind="NewUpdate.aspx.cs" Inherits="MachineUpdateFrontEnd.NewUpdate" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" DataKeyNames="UpdateID" style="margin-top: 0px" ShowHeaderWhenEmpty ="true">
<Columns>
<asp:BoundField DataField="UpdateID" HeaderText="UpdateID" InsertVisible="False" ReadOnly="True" SortExpression="UpdateID" />
<asp:BoundField DataField="MachineID" HeaderText="MachineID" SortExpression="MachineID" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID ="UpdateSqlDataSource" runat ="server" ConnectionString="<%$ ConnectionStrings:MachineUpdateDataBaseConnectionString %>" SelectCommand="SELECT * FROM [MachineUpdate MQ]">
</asp:SqlDataSource>
</asp:Content>
整个项目:
答案 0 :(得分:4)
我没有看到你将gridview与像
这样的数据源绑定GridView1.DataSource = input.Tables[0];
GridView1.DataBind(); //missing data binding