Hi Amol111,
Check this example. Now please take its reference and correct your code.
HTML
<table>
<tr>
<td>
<asp:GridView ID="grdDetails" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
Complete :
<asp:CheckBox ID="Chk_Complete" Checked='<%#Eval("Completed") %>' runat="server" />
<br />
Employee Id :
<asp:Label ID="lblEmpl_Id" Text='<%#Eval("EmployeeId") %>' runat="server" />
<br />
Date :
<asp:Label ID="lbl_Initiation_Date" Text='<%#Convert.ToDateTime(Eval("Date")).ToString("dd/MM/yyyy") %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td>
<asp:Label ID="lblTable" runat="server" />
</td>
</tr>
</table>
<br />
<asp:Button Text="Save" runat="server" OnClick="Save" />
Namespaces
C#
using System.Collections.Specialized;
using System.Data;
using System.Text;
VB.Net
Imports System.Collections.Specialized
Imports System.Data
Imports System.Text
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("EmployeeId", typeof(int)),
new DataColumn("Date", typeof(DateTime)),
new DataColumn("Completed",typeof(bool)) });
dt.Rows.Add(1, DateTime.Now, false);
dt.Rows.Add(2, DateTime.Now.AddDays(1), false);
dt.Rows.Add(3, DateTime.Now.AddDays(5), false);
dt.Rows.Add(4, DateTime.Now.AddDays(-10), false);
grdDetails.DataSource = dt;
grdDetails.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
for (int i = 0; i < grdDetails.Rows.Count; i++)
{
CheckBox checkbox = (CheckBox)grdDetails.Rows[i].Cells[0].FindControl("Chk_Complete");
Label lblEmpl_Id = (Label)grdDetails.Rows[i].Cells[0].FindControl("lblEmpl_Id");
Label lbl_Initiation_Date = (Label)grdDetails.Rows[i].Cells[0].FindControl("lbl_Initiation_Date");
if (checkbox != null)
{
if (checkbox.Checked)
{
string EmpId = lblEmpl_Id.Text;
string Trn_Initiation_Date = lbl_Initiation_Date.Text;
sc.Add(EmpId);
sc.Add(Trn_Initiation_Date);
}
}
}
DataTable dtItem_Completion = new DataTable();
dtItem_Completion.Columns.Add("ID", typeof(int));
dtItem_Completion.Columns.Add("EmpId", typeof(int));
dtItem_Completion.Columns.Add("Trn_Initiation_Date", typeof(string));
StringBuilder sb = new StringBuilder();
sb.Append("<table><tr><th>Id</th><th>Employee Id</th><th>Date</th></tr>");
DataRow dr = null;
int j = 0;
for (int i = 0; i < sc.Count / 2; i++)
{
sb.Append("<tr>");
dr = dtItem_Completion.NewRow();
dr["ID"] = i + 1;
sb.Append("<td>" + (i + 1) + "</td>");
dr["EmpId"] = sc[j];
sb.Append("<td>" + sc[j] + "</td>");
j++;
dr["Trn_Initiation_Date"] = sc[j];
sb.Append("<td>" + sc[j] + "</td>");
j++;
dtItem_Completion.Rows.Add(dr);
sb.Append("</tr>");
}
sb.Append("</table>");
lblTable.Text = sb.ToString();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("EmployeeId", GetType(Integer)),
New DataColumn("Date", GetType(DateTime)),
New DataColumn("Completed", GetType(Boolean))})
dt.Rows.Add(1, DateTime.Now, False)
dt.Rows.Add(2, DateTime.Now.AddDays(1), False)
dt.Rows.Add(3, DateTime.Now.AddDays(5), False)
dt.Rows.Add(4, DateTime.Now.AddDays(-10), False)
grdDetails.DataSource = dt
grdDetails.DataBind()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim sc As StringCollection = New StringCollection()
For i As Integer = 0 To grdDetails.Rows.Count - 1
Dim checkbox As CheckBox = CType(grdDetails.Rows(i).Cells(0).FindControl("Chk_Complete"), CheckBox)
Dim lblEmpl_Id As Label = CType(grdDetails.Rows(i).Cells(0).FindControl("lblEmpl_Id"), Label)
Dim lbl_Initiation_Date As Label = CType(grdDetails.Rows(i).Cells(0).FindControl("lbl_Initiation_Date"), Label)
If checkbox IsNot Nothing Then
If checkbox.Checked Then
Dim EmpId As String = lblEmpl_Id.Text
Dim Trn_Initiation_Date As String = lbl_Initiation_Date.Text
sc.Add(EmpId)
sc.Add(Trn_Initiation_Date)
End If
End If
Next
Dim dtItem_Completion As DataTable = New DataTable()
dtItem_Completion.Columns.Add("ID", GetType(Integer))
dtItem_Completion.Columns.Add("EmpId", GetType(Integer))
dtItem_Completion.Columns.Add("Trn_Initiation_Date", GetType(String))
Dim sb As StringBuilder = New StringBuilder()
sb.Append("<table><tr><th>Id</th><th>Employee Id</th><th>Date</th></tr>")
Dim dr As DataRow = Nothing
Dim j As Integer = 0
For i As Integer = 0 To sc.Count / 2 - 1
sb.Append("<tr>")
dr = dtItem_Completion.NewRow()
dr("ID") = i + 1
sb.Append("<td>" & (i + 1) & "</td>")
dr("EmpId") = sc(j)
sb.Append("<td>" & sc(j) & "</td>")
j += 1
dr("Trn_Initiation_Date") = sc(j)
sb.Append("<td>" & sc(j) & "</td>")
j += 1
dtItem_Completion.Rows.Add(dr)
sb.Append("</tr>")
Next
sb.Append("</table>")
lblTable.Text = sb.ToString()
End Sub
Screenshot
Note: Since in StringCollection adding 2 columns i have devided the for loop with 2. So change code according to your column.