Use the below Code
Default3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkAllocate" Checked="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
</html>
Default3.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable("tbl1");
dt.Columns.Add("ID");
dt.Columns.Add("Name");
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["ID"] = "ROW " + i.ToString();
dr["Name"] = "Name" + i.ToString();
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dtRoom = null;
DataRow datarw = null;
dtRoom = new DataTable();
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox check = (CheckBox)row.FindControl("chkAllocate");
if (check.Checked == true)
{
datarw = dtRoom.NewRow();
for (int j = 1; j < row.Cells.Count; j++)
{
if (dtRoom.Columns.Count != row.Cells.Count - 1)
dtRoom.Columns.Add("C" + j.ToString());
datarw[j - 1] = row.Cells[j].Text;
}
dtRoom.Rows.Add(datarw);
}
}
GridView1.DataSource = dtRoom;
GridView1.DataBind();
}
}