用于在gridview中显示数据的样式

时间:2017-05-16 09:56:06

标签: c# mysql asp.net gridview

我正在尝试在c#中的Gridview中显示数据。我被要求使gridview看起来像SQL数据库中的表一样接近。我已经能够实现一点但我的网格远远不是一个Sql表。有人可以帮我吗?这是我到目前为止所尝试的。

.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Reports.aspx.cs" Inherits="Reports" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Enter Query"></asp:Label> &nbsp;&nbsp;&nbsp; <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
        <p>
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
            <div id="grdCharges" runat="server" style="width: 875px; overflow: auto; height: 600px;">
                <asp:GridView ID="grd1" runat="server" AutoGenerateColumns="true" Width="150%">
                </asp:GridView>

            </div>
        </p>
    </form>
</body>
</html>

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;


public partial class Reports : System.Web.UI.Page
{

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = null;
        SqlDataReader reader = null;

        try
        {
            string q = TextBox1.Text.ToString();
            string strcon = "Data Source=***********";
            con = new SqlConnection(strcon);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = q;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;

            con.Open();

            reader = cmd.ExecuteReader();
            // Data is accessible through the DataReader object here.
            var dataTable = new DataTable();
            dataTable.Load(reader);
            grd1.DataSource = dataTable;
            grd1.DataBind();   
        }
        catch (Exception ex)
        {
            //....
        }
        finally
        {
            if(reader != null)
                reader.Close();

            if(con != null && con.ConnectionState != ConnectionState.Closed)
                con.Close();
        }
    }
}

这就是我的表格 - 当前表格 - Current table

目标表 - Targettable

0 个答案:

没有答案