手动绑定数据的GridView [英] Manually binding data to Gridview
问题描述
我要我的SQL领域绑定到我的GridView列。我前一阵子做这个和它的工作很好,但我忘了如何做到这一点,所以我让ASP.NET自动生成的列和它的作品,现在我想控制数据绑定,下面是我背后的代码和我的GridView。 ..任何援助将不胜感激。
保护无效的Page_Load(对象发件人,EventArgs五)
{
SqlConnection的康恩=新的SqlConnection(Sitecore.Configuration.Settings.GetConnectionString(反馈意见));
的SqlCommand CMD =新的SqlCommand(康涅狄格州,从fb_results选择*);
的DataSet DS =新的DataSet();
SqlDataAdapter的大=新SqlDataAdapter的(CMD);
da.Fill(DS);
GridView1.DataSource = DS;
GridView1.DataBind();
conn.Close();
}
GridView的:
<头ID =头像1=服务器>
<标题>反馈和LT; /标题>
< /头>
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =false的>
<柱体和GT;
< ASP:BoundField的数据字段=rpt_login的HeaderText =用户ID/>
< ASP:BoundField的数据字段=fb_url的HeaderText =URL ___/>
< ASP:BoundField的数据字段=fb_response的HeaderText =答:你有没有找到你要找的东西? />
< ASP:BoundField的数据字段=fb_noResponse的HeaderText =无反应或忽略/>
< ASP:BoundField的数据字段=fb_date的HeaderText =日期/>
< ASP:BoundField的数据字段=fb_serviceCall的HeaderText =防止的服务呼叫/>
< ASP:BoundField的数据字段=fb_partsShipment的HeaderText =防止零件出货/>
< ASP:BoundField的数据字段=fb_warranty的HeaderText =在保修/>
< ASP:BoundField的数据字段=fb_cancel的HeaderText =已取消/>
< ASP:BoundField的数据字段=fb_none的HeaderText =以上皆非/>
< /专栏>
< / ASP:GridView的>
确定。因此,对于评论:
这是我的亲身经历。我不得不返回此SQL查询:
| ---------------- ------------------------------- |
|第1栏|列2 |第3栏|
| --------------- | --------------- | ------------- - |
|c1foor1bar|c2foor1bar|c3foor1bar|
|c1foor2bar|c2foor2bar|c3foor2bar|
|c1foor3bar|c2foor3bar|c3foor3bar|
| --------------- | --------------- | ------------- - |
我的aspx页面是这样的:
< ASP:GridView控件=服务器ID =GridView1的AutoGenerateColumns =false的>
< ASP:BoundField的=服务器数据字段=strFirst>< / ASP:BoundField的>
< ASP:BoundField的=服务器数据字段=strLast>< / ASP:BoundField的>
< / ASP:GridView的>
和我的页面加载是这样的:
保护无效的Page_Load(对象发件人,EventArgs五)
{
SqlConnection的康恩=新的SqlConnection(小于的信息>);
的SqlCommand CMD =新的SqlCommand(SELECT * FROM fb_results,康恩);
DataTable的DT =新的DataTable();
SqlDataAdapter的大=新SqlDataAdapter的(CMD);
da.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
conn.Close();
}
有两个问题。首先,我的专栏还不能称作是相同的。我可以很容易地改变它们,但它们确实代表了不同的数据,所以我并不想这样做。其次,我带来了太多的数据。我的解决办法是重建表:
保护无效的Page_Load(对象发件人,EventArgs五)
{
SqlConnection的康恩=新的SqlConnection(小于的信息>);
的SqlCommand CMD =新的SqlCommand(SELECT * FROM fb_results,康恩);
DataTable的DT =新的DataTable();
SqlDataAdapter的大=新SqlDataAdapter的(CMD);
da.Fill(DT);
DataTable中newtable的= createDataTableTemplate();
的foreach(在dt.Rows的DataRow DR){
的DataRow NEWROW = newTable.NewRoW();
NEWROW [strFirst] = SomeOperation((字符串)博士[列1]);
NEWROW [strLast] = SomeOtherOperation((字符串)博士[列2]);
newTable.Rows.Add(NEWROW);
}
GridView1.DataSource = newtable的;
GridView1.DataBind();
conn.Close();
}
私人数据表createDataTableTemplate()
{
DataTable的表=新的DataTable(表标题);
的DataColumn COL1 =新的DataColumn(strFirst);
col1.DataType = System.Type.GetType(System.String);
的DataColumn COL2 =新的DataColumn(strLast);
col2.DataType = System.Type.GetType(System.String);
table.Columns.Add(COL1);
table.Columns.Add(COL2);
返回表;
}
请注意:的DataSet
不使用,并且所有的BoundField
活动的 =服务器
在其中。
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>
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 BoundField
s have runat="server"
in them.
这篇关于手动绑定数据的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!