[英]transfer data in another gridview
I have a gridview with these columns: id,name,price,quantity,total. 我有一个包含以下列的gridview:id,名称,价格,数量,总计。 Also i have a button and checkbox in every row of gridview.
我在gridview的每一行中都有一个按钮和复选框。 When i check some rows i would like these rows with the button to transfer in another gridview.How can i do that?
当我检查一些行时,我希望这些行与按钮一起在另一个gridview中传输。我该怎么做?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource3">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
<asp:HiddenField ID="hdValue" runat="server" Value='<%#Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" ReadOnly="True"
SortExpression="name" />
<asp:BoundField DataField="price" DataFormatString="{0:c}" HeaderText="price"
ReadOnly="True" SortExpression="price" />
<asp:BoundField DataField="quantity" HeaderText="quantity"
SortExpression="quantity" />
<asp:BoundField DataField="total" DataFormatString="{0:c}" HeaderText="total"
ReadOnly="True" SortExpression="total" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Land]"></asp:SqlDataSource>
</div>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
While working with data grids, I usually create a class which hold a template for my data, and then I create objects from this class and put in a list, then I can easily bind this list to any grid. 在使用数据网格时,通常会创建一个类,该类包含用于数据的模板,然后从该类中创建对象并放入列表中,然后可以轻松地将此列表绑定到任何网格。
For example: 例如:
public class LandTemplate
{
public property Id{get; set;}
public property Name{get; set;}
public property Price{get; set;}
public property Quantity{get; set;}
public property Total{get; set;}
}
Then in your method which in your case will be the click on check box event you can do that: 然后,在您的方法中(如果您选择的是单击复选框事件),您可以执行以下操作:
List<LandTemplate> landList = new List<LandTemplate>();
public void CheckBox_Click(object sender, EventArgs e)
{
// get data from the current grid row...
LandTemplate land = new LandTemplate();
land.Id= currentRow.Id;
land.Name = currentRow.Name;
....
landList.Add(land);
}
public void Button_Click(object sender, EventArgs e)
{
this.gridView1.DataSource = landList;
this.gridView1.DataBind();
}
But you will need to save the LandList in a viewstate or session to don't loose data inside after every page refresh. 但是您将需要将LandList保存在视图状态或会话中,以免在每次刷新页面后丢失内部数据。
I didn't check for syntax, but I hope you got the idea. 我没有检查语法,但希望您能理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.