简体   繁体   English

如何将字符串绑定到网格?

[英]How to bind string to grid?

I want to display data in gridview based on check box selection from another grid view. 我想根据另一个网格视图中的复选框选择在gridview中显示数据。 The given below code getting values into from first gridview based on check box selection. 下面给出的代码根据复选框的选择将值从第一个gridview中获取。 I want to bind that values into second grid. 我想将该值绑定到第二个网格中。 Help me to find a proper solution. 帮助我找到适当的解决方案。 Thank you. 谢谢。

Code : 代码:

protected void btnAssign_Click(object sender, EventArgs e)
   {
    foreach (GridViewRow row in GridView1.Rows)
       {
        if (row.RowType == DataControlRowType.DataRow)
            {
               CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
               if (chkRow.Checked)
                {
                    string[] EmpId = new string[] { row.Cells[0].Text };
                    string[] EmpName = new string[] { row.Cells[1].Text};

// I want to display emp id and emp name on gridview 2 based on check box selection. //我想基于复选框选择在GridView 2上显示emp id和emp名称。 How can I do. 我能怎么做。 Help me to find a proper solution 帮我找到合适的解决方案

                    GridView2.DataSource = EmpId;
                    GridView2.DataBind();
                }

            }

        }
   }

ASPX: ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="Employee Name" ReadOnly="True" SortExpression="FirstName" />


<asp:TemplateField HeaderText="Select" SortExpression="Select">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:GridView ID="GridView2" runat="server"> </asp:GridView>

The given below image is showing data in grid view based on drop-down selection. 下图给出了基于下拉选择的网格视图中的数据。 After that I want to select some rows and click assign then I have to display the selected rows in new gridview. 之后,我想选择一些行并单击“分配”,然后我必须在新的gridview中显示选定的行。 Image : 图片 :

在此处输入图片说明

If you don't already have a strong type defined to represent your selected Employee, you could create a Dictionary to store your selected employees. 如果尚未定义强类型来表示所选雇员,则可以创建字典来存储所选雇员。 Upon completing your iteration over GridView1's rows to get the selected employees, you could use the dictionary as the datasource for GridView2 and bind to the Key and Value of each entry. 完成对GridView1的行的迭代以获取选定的雇员后,可以将字典用作GridView2的数据源,并绑定到每个条目的键和值。

protected void btnAssign_Click(object sender, EventArgs e)
{
    Dictionary<int, string> selectedEmployees = new Dictionary<int, string>();
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
           CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
           if (chkRow.Checked)
            {
                selectedEmployees.Add(int.Parse(row.Cells[0].Text), row.Cells[1].Text);
            }
        }
    }

    if (selectedEmployees.Any())
    {
        gridView2.DataSource = selectedEmployees;
        gridView2.DataBind();
    }

} }

UPDATE: Updated the code to account for the exception that you were receiving with the sample code provided 更新:更新了代码以解决您通过提供的示例代码收到的异常

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

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