简体   繁体   English

如何在GridView中填充选定的列并从后面的代码更新?

[英]How to populated selected columns in GridView and update from code behind?

I have a GridView1 , which I am binding from code behind. 我有一个GridView1 ,我从后面的代码绑定。 The one of the columns in the GridView depends on Label1.Text as follows: GridView的一列依赖于Label1.Text ,如下所示:

SqlCommand comd = new SqlCommand("SELECT Location_Profile_Name, " + Label1.Text + " FROM Home_Profile_Master", con);
SqlDataAdapter da = new SqlDataAdapter(comd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

The aspx code for the same is : 同样的aspx代码是:

<asp:TemplateField HeaderText="Location_Profile_Name" 
SortExpression="Location_Profile_Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" 
Text='<%# Bind("Location_Profile_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Home_Profile" SortExpression="Label10">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

I am getting an error in the aspx page as: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Home_Profile' . 我在aspx页面中收到错误: DataBinding:'System.Data.DataRowView'不包含名为'Home_Profile'的属性

I am not able to figure out what the mistake is. 我无法弄清楚错误是什么。 Kindly Help...! 请帮助......! thank you. 谢谢。

你shuld在数据表中有Home_Profile列试试这个

 SqlCommand comd = new SqlCommand("SELECT Location_Profile_Name,Home_Profile, " + Label1.Text + " FROM Home_Profile_Master", con);

你错过了查询中的'Home_Profile'。

SqlCommand comd = new SqlCommand("SELECT Location_Profile_Name," + Label1.Text + " as Home_Profile FROM Home_Profile_Master", con);

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

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