Hi khaled2016,
I have created sample code which full fill your requirement.
HTML
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" OnCheckedChanged="OnChange" AutoPostBack="true" runat="server" />
<br />
person_no:
<asp:Label ID="personnoLabel" runat="server" Text='<%# Eval("person_no") %>' />
<br />
person_name:
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("person_name") %>' />
<br />
Country_no:
<asp:Label ID="countrynoLabel" runat="server" Text='<%# Eval("Country_no") %>' />
<br />
</ItemTemplate>
</asp:DataList>
<br />
<asp:GridView ID="gvDetails" runat="server" />
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetData();
this.DummyDataTable();
}
}
private void DummyDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("PersonNo");
dt.Columns.Add("PersonName");
dt.Columns.Add("Country");
ViewState["CheckedData"] = dt;
}
private void GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("person_no");
dt.Columns.Add("person_name");
dt.Columns.Add("Country_no");
dt.Rows.Add("1", "PETER", "2514");
dt.Rows.Add("2", "John", "21414");
dt.Rows.Add("3", "Maria", "82947");
dt.Rows.Add("4", "Mahi", "251744");
DataList1.DataSource = dt;
DataList1.DataBind();
}
protected void OnChange(object sender, EventArgs e)
{
DataTable checkeddt = (DataTable)ViewState["CheckedData"];
DataListItem item = (sender as CheckBox).NamingContainer as DataListItem;
string personNo = (item.FindControl("personnoLabel") as Label).Text;
string personName = (item.FindControl("nameLabel") as Label).Text;
string country = (item.FindControl("countrynoLabel") as Label).Text;
if ((item.FindControl("CheckBox1") as CheckBox).Checked)
{
checkeddt.Rows.Add(personNo, personName, country);
this.GetcheckedDetails();
}
else
{
this.DummyDataTable();
DataTable dt = (DataTable)ViewState["CheckedData"];
foreach (DataRow row in checkeddt.Rows)
{
string personId = row["PersonNo"].ToString();
if (personId != personNo)
{
dt.Rows.Add(row["PersonNo"].ToString(), row["PersonName"].ToString(), row["Country"].ToString());
}
}
this.GetcheckedDetails();
}
}
private void GetcheckedDetails()
{
DataTable checkeddt = (DataTable)ViewState["CheckedData"];
gvDetails.DataSource = checkeddt;
gvDetails.DataBind();
}
Vb.net
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Me.GetData()
Me.DummyDataTable()
End If
End Sub
Private Sub DummyDataTable()
Dim dt As New DataTable()
dt.Columns.Add("PersonNo")
dt.Columns.Add("PersonName")
dt.Columns.Add("Country")
ViewState("CheckedData") = dt
End Sub
Private Sub GetData()
Dim dt As New DataTable()
dt.Columns.Add("person_no")
dt.Columns.Add("person_name")
dt.Columns.Add("Country_no")
dt.Rows.Add("1", "PETER", "2514")
dt.Rows.Add("2", "John", "21414")
dt.Rows.Add("3", "Maria", "82947")
dt.Rows.Add("4", "Mahi", "251744")
DataList1.DataSource = dt
DataList1.DataBind()
End Sub
Protected Sub OnChange(sender As Object, e As EventArgs)
Dim checkeddt As DataTable = DirectCast(ViewState("CheckedData"), DataTable)
Dim item As DataListItem = TryCast(TryCast(sender, CheckBox).NamingContainer, DataListItem)
Dim personNo As String = TryCast(item.FindControl("personnoLabel"), Label).Text
Dim personName As String = TryCast(item.FindControl("nameLabel"), Label).Text
Dim country As String = TryCast(item.FindControl("countrynoLabel"), Label).Text
If TryCast(item.FindControl("CheckBox1"), CheckBox).Checked Then
checkeddt.Rows.Add(personNo, personName, country)
Me.GetcheckedDetails()
Else
Me.DummyDataTable()
Dim dt As DataTable = DirectCast(ViewState("CheckedData"), DataTable)
For Each row As DataRow In checkeddt.Rows
Dim personId As String = row("PersonNo").ToString()
If personId <> personNo Then
dt.Rows.Add(row("PersonNo").ToString(), row("PersonName").ToString(), row("Country").ToString())
End If
Next
Me.GetcheckedDetails()
End If
End Sub
Private Sub GetcheckedDetails()
Dim checkeddt As DataTable = DirectCast(ViewState("CheckedData"), DataTable)
gvDetails.DataSource = checkeddt
gvDetails.DataBind()
End Sub
Screenshot