简体   繁体   English

手动绑定数据到 Gridview

[英]Manually binding data to Gridview

I need to bind my SQL fields to my Gridview column.我需要将我的 SQL 字段绑定到我的 Gridview 列。 I did this a while ago and it worked great, but I forgot how to do this, so I let ASP.NET AutoGenerate the columns and it works, now I want to control the data binding, below is my code behind and my Gridview... any assistance will be appreciated.我前一段时间做了这个,效果很好,但我忘记了如何做到这一点,所以我让 ASP.NET AutoGenerate 列并且它起作用了,现在我想控制数据绑定,下面是我的代码和我的 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>

OK.行。 So, regarding the comments:所以,关于评论:

This is my personal experience.这是我的个人经验。 I had a SQL query that returned this:我有一个返回这个的 SQL 查询:

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

My aspx page looked like this:我的 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>

And my pageload looked like this:我的页面加载看起来像这样:

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();
}

There were two problems.有两个问题。 First, my columns weren't called the same.首先,我的专栏的名称不同。 I could easily change them, but they really did represent different data so I didn't want to do that.我可以很容易地改变它们,但它们确实代表了不同的数据,所以我不想这样做。 Second, I was bringing in too much data.其次,我带来了太多的数据。 My solution was to rebuild the table:我的解决方案是重建表:

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;
}

Please note: DataSet is not used, and all BoundField s have runat="server" in them.请注意:未使用DataSet ,并且所有BoundField都有runat="server"

templistClient = (from PTerm in objResponseLocal.GetPaymentTermResponseData.PaymentTerms where PTerm.OrganisationSupplierContractID == null select PTerm).ToList(); templistClient = (来自 objResponseLocal.GetPaymentTermResponseData.PaymentTerms 中的 PTerm 其中 PTerm.OrganisationSupplierContractID == 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM