gridview绑定linqdatasource后获取gridview中的字段
2017-06-16 19:13
267 查看
关于如何从绑定数据源控件的gridview查询的结果中获取其中的一个字段,网上找了很多方法,这是本人最终的解决办法
界面设计:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="find" runat="server" OnClick="find_Click" Text="find" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ThemeId" DataSourceID="LinqDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ThemeId" HeaderText="ThemeId" InsertVisible="False" ReadOnly="True" SortExpression="ThemeId" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Context" HeaderText="Context" SortExpression="Context" />
<asp:CheckBoxField DataField="Screen" HeaderText="Screen" SortExpression="Screen" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
<asp:TemplateField HeaderText="获取id">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="CardDataContext" EntityTypeName="" TableName="Theme" EnableUpdate="True" EnableDelete="True" Where="ThemeId == @ThemeId">
<WhereParameters>
<asp:Parameter DefaultValue="3" Name="ThemeId" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 21px" Text="确定" />
先选定获取id列中的checkbok然后点击确定按钮后在label中会获取本行的ThemeId的值
具体的方法代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
//id = (Label)gvRow.FindControl("Label1");
//((Label)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim();
CheckBox CheckBox2 = (CheckBox)gvRow.FindControl("CheckBox2");//FindControl(String)使用指定的服务器控件当前命名容器中搜索 id 参数。
if (CheckBox2.Checked)
{
Label2.Text = gvRow.Cells[0].Text;
}
}
}
这样就能获取gridview中查询的数据的字段了。
界面设计:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="find" runat="server" OnClick="find_Click" Text="find" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ThemeId" DataSourceID="LinqDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ThemeId" HeaderText="ThemeId" InsertVisible="False" ReadOnly="True" SortExpression="ThemeId" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Context" HeaderText="Context" SortExpression="Context" />
<asp:CheckBoxField DataField="Screen" HeaderText="Screen" SortExpression="Screen" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
<asp:TemplateField HeaderText="获取id">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="CardDataContext" EntityTypeName="" TableName="Theme" EnableUpdate="True" EnableDelete="True" Where="ThemeId == @ThemeId">
<WhereParameters>
<asp:Parameter DefaultValue="3" Name="ThemeId" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 21px" Text="确定" />
先选定获取id列中的checkbok然后点击确定按钮后在label中会获取本行的ThemeId的值
具体的方法代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
//id = (Label)gvRow.FindControl("Label1");
//((Label)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim();
CheckBox CheckBox2 = (CheckBox)gvRow.FindControl("CheckBox2");//FindControl(String)使用指定的服务器控件当前命名容器中搜索 id 参数。
if (CheckBox2.Checked)
{
Label2.Text = gvRow.Cells[0].Text;
}
}
}
这样就能获取gridview中查询的数据的字段了。
相关文章推荐
- 绑定SqlDataSource的Gridview字符串字段长度截取
- 绑定SqlDataSource的Gridview字符串字段长度截取
- 绑定SqlDataSource的Gridview字符串字段长度截取
- gridview 实现排序 (在不是使用sqlDataSource控件,而在后台编码绑定gridview时,指定那个字段排序时使用。本例用了单层结构,可修改后应用于多层)
- 获取GridView绑定的数据值(没显示出来的字段)
- 绑定SqlDataSource的Gridview字符串字段长度截取(转)
- ALinq 使用教程(九)绑定数据到 GridView (LinqDataSource 使用)
- 获取设置gridview的绑定字段
- 在GridView1的RowEditing获取GridView1里面绑定的某一字段
- aspxgridview获取选中行内某个字段的值
- 获取gridview上的索引,从而得到绑定的值
- Ajax实现无刷新的获取数据并绑定到GridView,以及无刷新更新数据
- GridView模板列绑定字段过长的解决方案
- Gridview 模板列绑定数据库字段
- asp Gridview绑定形式获取行号
- GridView 动态添加模板列并绑定数据 实现从外部直接传入控件 和 绑定字段参数
- Linq查询绑定给GridView,提示 数据源不支持服务器端的数据分页.
- 用datasource来绑定gridview如何进行修改,更新,删除
- Linq to Oracle 使用教程(十)绑定数据到 GridView
- GridView中itemTemplate绑定字段的几种方式