Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在GridView中填充选定列并从代码隐藏中更新?_C#_Asp.net_Gridview - Fatal编程技术网

C# 如何在GridView中填充选定列并从代码隐藏中更新?

C# 如何在GridView中填充选定列并从代码隐藏中更新?,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView1,我正在从代码背后绑定它。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

我有一个
GridView1
,我正在从代码背后绑定它。
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();
相同的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>

我在aspx页面中遇到错误:数据绑定:“System.Data.DataRowView”不包含名为“Home\u Profile”的属性。


我想不出是什么错误。请帮忙。。。!谢谢。

您应该在数据表中设置Home\u Profile列。请尝试此操作

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

您在查询中遗漏了“主页配置文件”

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

您确定这个
SqlCommand(“选择位置\u配置文件\u名称,+Label1.Text+”来自主页\u配置文件\u主文件”,con)吗?无法
Label1.Text
go rough?:)是的,基于
Label1。Text
是我要填充的另一个数据克隆。这不会在我的网格中生成3列吗?它表示无效的列名Home\u Profile。我在数据库中没有Home_Profile列它不会在gridView中生成三列,因为您已经明确声明了项模板,而且您提到了“”,但您没有从查询中向该列提供数据,那么在我的模板中,我是否应该将Home_Profile替换为Label1?好的..那么Label1.Text是什么?您可以将其命名为以下内容…SqlCommand comd=newsqlcommand(“从主配置文件主配置文件中选择位置配置文件名称,+Label1.Text+”作为主配置文件”,con);这不会在我的网格中生成3列吗?它表示无效的列名Home\u Profile。我在数据库中没有Home_Profile列