手动将数据绑定到Gridview

本文关键字:Gridview 数据绑定 | 更新日期: 2023-09-27 18:19:19

我需要绑定我的SQL字段到我的Gridview列。我不久前这样做了,它工作得很好,但我忘记了如何做到这一点,所以我让ASP。. NET自动生成列,它的工作原理,现在我想控制数据绑定,下面是我的代码背后和我的Gridview…如有任何协助,不胜感激。

 protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(Sitecore.Configuration.Settings.GetConnectionString("feedback"));
            SqlCommand cmd = new SqlCommand("select * from fb_results", conn);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            conn.Close();
        }
Gridview:

<head id="Head1" runat="server">
    <title>Feedback</title>
</head>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="rpt_login" HeaderText="User Id" />
        <asp:BoundField DataField="fb_url" HeaderText="URL___" />
        <asp:BoundField DataField="fb_response" HeaderText="Answer: Did you find what you were looking for?" />
        <asp:BoundField DataField="fb_noResponse" HeaderText="No Response or Ignore" />
        <asp:BoundField DataField="fb_date" HeaderText="Date" />
        <asp:BoundField DataField="fb_serviceCall" HeaderText="Prevented Service Call" />
        <asp:BoundField DataField="fb_partsShipment" HeaderText="Prevented Parts Shipment" />
        <asp:BoundField DataField="fb_warranty" HeaderText="Under Warranty" />
        <asp:BoundField DataField="fb_cancel" HeaderText="Cancelled" />
        <asp:BoundField DataField="fb_none" HeaderText="None of the Above" />
    </Columns>
</asp:GridView>

手动将数据绑定到Gridview

OK。那么,关于注释:

这是我的个人经历。我有一个SQL查询,返回如下:

|-----------------------------------------------|
|Column 1       |Column 2       |Column 3       |
|---------------|---------------|---------------|
|"c1foor1bar"   |"c2foor1bar"   |"c3foor1bar"   |
|"c1foor2bar"   |"c2foor2bar"   |"c3foor2bar"   |
|"c1foor3bar"   |"c2foor3bar"   |"c3foor3bar"   |
|---------------|---------------|---------------|

我的aspx页面是这样的:

<asp:GridView runat="server" id="GridView1" AutoGenerateColumns="false">
    <asp:BoundField runat="server" DataField="strFirst"></asp:BoundField>
    <asp:BoundField runat="server" DataField="strLast"></asp:BoundField>
</asp:GridView>

我的页面加载是这样的:

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(<the info>);
    SqlCommand cmd = new SqlCommand("SELECT * FROM fb_results", conn);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    conn.Close();
}

有两个问题。首先,我的专栏名字不一样。我可以很容易地改变它们,但它们确实代表不同的数据,所以我不想这样做。其次,我带来了太多的数据。我的解决方案是重建表:

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(<the info>);
    SqlCommand cmd = new SqlCommand("SELECT * FROM fb_results", conn);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    DataTable newTable = createDataTableTemplate();
    foreach (DataRow dr in dt.Rows) {
        DataRow newRow = newTable.NewRoW();
        newRow["strFirst"] = SomeOperation((String) dr["Column 1"]);
        newRow["strLast"] = SomeOtherOperation((String) dr["Column 2"]);
        newTable.Rows.Add (newRow);
    }
    GridView1.DataSource = newTable;
    GridView1.DataBind();
    conn.Close();
}
private DataTable createDataTableTemplate ()
{
    DataTable table = new DataTable("Table Title");
    DataColumn col1 = new DataColumn("strFirst");
    col1.DataType = System.Type.GetType("System.String");
    DataColumn col2 = new DataColumn("strLast");
    col2.DataType = System.Type.GetType("System.String");
    table.Columns.Add (col1);
    table.Columns.Add (col2);
    return table;
}

请注意:没有使用DataSet,所有BoundField中都有runat="server"

templistClient = (from PTerm in objresponsellocal . getpaymenttermresponsedata . paymentterms .PTerm的地方。organisationsuppliercontracactid == null选择PTerm) .ToList ();

                    DataTable dt = grdPaymentTerms.ToDataTable(templistClient);
                    string strPaymentType = "";
                    dt.Columns.Add(new DataColumn("Paymenttermselectedpaymenttype", strPaymentType.GetType()));
                    foreach (DataRow dr in dt.Rows)
                    {
                        dr["Paymenttermselectedpaymenttype"] = (Convert.ToInt32(dr["Paymenttermpaymenttype"]) == 1 ? "Final Payment" : "Deposit Payment");
                        dr["Paymenttermamount"] = (Convert.ToInt32(dr["Paymenttermpaymenttype"]) == 1 ? DBNull.Value : dr["Paymenttermamount"]);
                        dr["Type"] = (Convert.ToInt32(dr["Paymenttermpaymenttype"]) == 1 ? DBNull.Value : dr["Type"]);
                    }

                    grdPaymentTerms.DataBind(dt);