Manually binding data to Gridview

吃可爱长大的小学妹 提交于 2020-01-03 06:20:34

问题


I need to bind my SQL fields to my Gridview column. 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.

 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>

回答1:


OK. So, regarding the comments:

This is my personal experience. I had a SQL query that returned this:

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

My aspx page looked like this:

<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 BoundFields have runat="server" in them.



来源:https://stackoverflow.com/questions/17239942/manually-binding-data-to-gridview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!