Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a grid view. it contains 5 column and first columns contain check box in each row on record. how to do this if i am check on first row than only first row save in table.. Means Check row save in table.. please help...
and another question is this is my code i want to do this only those row add in datatable.. but it gives an Error "Cannot column find 0" if i change the value of j=1 it gives again an error ""Cannot column find 1"

for (int i = 0; i <= gwRoom.Rows.Count; i++)
{
//drw = dtRoom.NewRow();
CheckBox check = (CheckBox)gwRoom.Rows[i].FindControl("chkAllocate");
if (check.Checked == true)
{
foreach (GridViewRow row in gwRoom.Rows)
{
DataRow datarw = null;
dtRoom = new DataTable();
datarw = dtRoom.NewRow();
for (int j = 1; j < row.Cells.Count; j++)
{
datarw[j] = row.Cells[j].Text;
}
dtRoom.Rows.Add(datarw);

}
gwRoom.DataSource=dtRoom;
gwRoom.DataBind();
}
}
Please please help me....
Posted

Use the below Code

Default3.aspx

XML
<%@ 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();
    }
}
 
Share this answer
 
try this
C#
for (int i = 0; i <= gwRoom.Rows.Count; i++)
{
//drw = dtRoom.NewRow();
CheckBox check = (CheckBox)gwRoom.Rows[i].FindControl("chkAllocate");
if (check.Checked == true)
{

DataRow datarw = null;
dtRoom = new DataTable();
datarw = dtRoom.NewRow();
for (int j = 1; j < gwRoom.Rows[i].Cells.Count; j++)
{
datarw[j] = gwRoom.Rows[i].Cells[j].Text;
}
dtRoom.Rows.Add(datarw);



}
}
gwRoom.DataSource=dtRoom;
gwRoom.DataBind();
 
Share this answer
 
Comments
Arsalaan Ahmed 17-Jul-13 5:23am    
its given an error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900