网格视图行添加数据表问题 [英] grid view row add in data table Problem
问题描述
我有一个网格视图。它包含5列和第一列包含记录中每行的复选框。怎么做如果我在第一行检查,而不是只有第一行保存在表中..手段检查行保存在表格中...请帮助...
和另一个问题是这是我的代码我想要只做那些行添加数据表..但它给出一个错误不能列找到0如果我改变j = 1的值它再次给出一个错误不能列找到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(gwRoom.Rows中的GridViewRow行)
{
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();
}
}
请帮助我....
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....
推荐答案
试试这个
try this
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();
使用以下代码
Default3.aspx
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
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();
}
}
这篇关于网格视图行添加数据表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!