我是C#ASP.NET的新手,我的项目包含2个GridViews,第一个GridView显示来自SQL Server的数据,并且工作正常。 现在我需要实现的是,当我们从gridView 1中选择任何行时,应将这些行复制到GridView 2中。
下面是我的代码:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=x;Integrated Security=True");
sc.Open();
SqlDataAdapter sd = new SqlDataAdapter("select * from BookDetails", sc);
DataSet ds = new DataSet();
sd.Fill(ds, "BookDetails");
sc.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
GridView.Rows.Add(row);
GridView.Rows.Remove(row);
}
}
GridView.Rows.Add(row); //错误 GridView.Rows.Remove(row); //错误 在表格中显示数据源的值,每一列代表一个字段,每一行代表一条记录
答案 0 :(得分:0)
问题是您根本不需要遍历每一行。因为您已经知道所选的行。尝试使用类似的东西:
GridViewRow row = GridView1.SelectedRow;
GridView2.DataSource = row;
GridView2.DataBind();
顺便说一下,以下代码将帮助您遍历每一行:
foreach (GridViewRow row in GridView1.Rows)
{
........
}
答案 1 :(得分:0)
使用它可能会对您有所帮助。
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
<asp:HiddenField ID="hdValue" runat="server" Value='<%#Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:Button ID="btnMove" runat="server" Text="Move" OnClick="btnMove_Click" />
</div>
<div>
<asp:GridView ID="gridView2" runat="server">
</asp:GridView>
</div>
</form>
</body>
c#代码
private void BindGridView()
{
if (Session[key] == null)
{
gridView1.DataSource = GetDataSource();
gridView1.DataBind();
}
else
{
gridView1.DataSource = (DataTable)Session[key];
gridView1.DataBind();
}
}
protected DataTable GetDataSource()
{
try
{
DataTable dt = new DataTable();
dt = new DataTable();
dt.Columns.Add("ID", typeof(int)).AutoIncrement = true;
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));
DataColumn[] keys = new DataColumn[2];
keys[0] = dt.Columns["ID"];
dt.PrimaryKey = keys;
dt.Rows.Add("1", "first object", 34);
dt.Rows.Add("2", "second object", 24);
dt.Rows.Add("3", "third object", 34);
dt.Rows.Add("4", "fourth object", 24);
dt.Rows.Add("5", "fifth object", 34);
Session[key] = dt;
return dt;
}
catch
{
return null;
}
}
protected void btnMove_Click(object sender, EventArgs e)
{
try
{
DataTable dtMain = Session[key] as DataTable;
//copy the schema of source table
DataTable dtClone = dtMain.Clone();
foreach (GridViewRow gv in gridView1.Rows)
{
CheckBox chk = gv.FindControl("chkSelect") as CheckBox;
HiddenField hdValue = gv.FindControl("hdValue") as HiddenField;
if (chk.Checked)
{
//get only the rows you want
DataRow[] results = dtMain.Select("ID=" + hdValue.Value + "");
//populate new destination table
foreach (DataRow dr in results)
{
dtClone.ImportRow(dr);
}
}
gridView2.DataSource = dtClone;
gridView2.DataBind();
}
}
catch
{
BindGridView();
}
}
答案 2 :(得分:0)
int x = 0;// this must be placed obove the dataGridView1_SelectionChanged
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
this.dataGridView3.Rows.Add();
this.dataGridView3.Rows[x].Cells[0].Value = row.Cells[0].Value.ToString();
x++;
}
}