EDIT: I forgot to bind the datatable (oops), that is now fixed. The program now crashes, and I am still confused as too why.
I am making a website where I want to display a data table that has had its rows and columns switch. I wrote some c# to do this. My code compiles and runs without crashing, but the gridview where I put this data never shows up. If I remove the code binding the data to the gridview, and instead use a sql source connection, it shows (but obviously is not flipped).
Why is my gridview not showing up and how would I go about fixing it?
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>
Entire project:
I don't see you are binding the gridview with the datasource like
GridView1.DataSource = input.Tables[0];
GridView1.DataBind(); //missing data binding
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.