问题是我正在两种不同形式上使用2个gridviews;第一个是显示数据库中的项目,并且工作正常。当我选择一行并单击一个按钮时,该行将转到第二个表单“购物车表单”上的gridview#2,将显示该行,但是当我关闭该表单或从gridview#1中选择另一行并想要检索它,已检索的第一行将被新的选择行替换!我如何将所有选择项保留在gridview 2中?
这是Grid View 1的代码
private void Buyer_Main_Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-CJGIQ74;Initial Catalog=Items;Integrated Security=True");
SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM list", con);
DataTable dt = new DataTable();
adp.Fill(dt);
foreach (DataRow item in dt.Rows)
{
int n = Gridview.Rows.Add();
Gridview.Rows[n].Cells[0].Value = item[0].ToString();
Gridview.Rows[n].Cells[1].Value = item["Name"].ToString();
Gridview.Rows[n].Cells[2].Value = item["Price"].ToString();
Gridview.Rows[n].Cells[3].Value = item["Quantity"].ToString();
}
}
用于将项目检索到Gridview 2的按钮的代码
private void button1_Click_1(object sender, EventArgs e)
{
Checkout datagrid = new Checkout(Gridview.SelectedRows[0].Cells[0].Value.ToString(),
Gridview.SelectedRows[0].Cells[1].Value.ToString(),
Gridview.SelectedRows[0].Cells[2].Value.ToString(),
Gridview.SelectedRows[0].Cells[3].Value.ToString());
datagrid.Show();
}
GridView 2以显示检索到的项目
public Checkout(string ID, string name, string price , string quantity)
{
InitializeComponent();
dataGridView1.Rows.Add();
dataGridView1.Rows[0].Cells[0].Value = ID;
dataGridView1.Rows[0].Cells[1].Value = name;
dataGridView1.Rows[0].Cells[2].Value = price;
dataGridView1.Rows[0].Cells[3].Value = quantity;
}
因此,基本上我想将检索到的数据保留在网格视图2中,以便用户可以选择要购买的商品并签出所选商品。另外,任何人都可以帮助我有关数量的信息,如何只选择一个将从我拥有的库存或库存中减去的数量
答案 0 :(得分:0)
第一个GridView
<asp:GridView ID="gvAll" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" AllowPaging ="true"
OnPageIndexChanging = "OnPaging" PageSize = "10" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);"
AutoPostBack = "true" OnCheckedChanged = "CheckBox_CheckChanged"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)"
AutoPostBack = "true" OnCheckedChanged = "CheckBox_CheckChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField = "CustomerID" HeaderText = "Customer ID"
HtmlEncode = "false" />
<asp:BoundField DataField = "ContactName" HeaderText = "Contact Name"
HtmlEncode = "false" />
<asp:BoundField DataField = "City" HeaderText = "City"
HtmlEncode = "false" />
</Columns>
<AlternatingRowStyle BackColor="#C2D69B" />
</asp:GridView>
辅助GridView
<asp:GridView ID="gvSelected" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" EmptyDataText = "No Records Selected" >
<Columns>
<asp:BoundField DataField = "CustomerID" HeaderText = "Customer ID" />
<asp:BoundField DataField = "ContactName" HeaderText = "Contact Name" />
<asp:BoundField DataField = "City" HeaderText = "City" />
</Columns>
</asp:GridView>
private void BindGridone()
{
string constr = ConfigurationManager
.ConnectionStrings["conString"].ConnectionString;
string query = " select CustomerID, ContactName, City from customers";
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
sda.Fill(dt);
gvAll.DataSource = dt;
gvAll.DataBind();
}
GetData函数只是检索用户已选中其复选框的记录,将它们添加到DataTable中,然后将DataTable保存到ViewState
private void GetData()
{
DataTable dt;
if (ViewState["SelectedRecords"] != null)
dt = (DataTable)ViewState["SelectedRecords"];
else
dt = CreateDataTable();
CheckBox chkAll = (CheckBox)gvAll.HeaderRow
.Cells[0].FindControl("chkAll");
for (int i = 0; i < gvAll.Rows.Count; i++)
{
if (chkAll.Checked)
{
dt = AddRow(gvAll.Rows[i], dt);
}
else
{
CheckBox chk = (CheckBox)gvAll.Rows[i]
.Cells[0].FindControl("chk");
if (chk.Checked)
{
dt = AddRow(gvAll.Rows[i], dt);
}
else
{
dt = RemoveRow(gvAll.Rows[i], dt);
}
}
}
ViewState["SelectedRecords"] = dt;
}
private DataTable CreateDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("CustomerID");
dt.Columns.Add("ContactName");
dt.Columns.Add("City");
dt.AcceptChanges();
return dt;
}
private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("CustomerID = '" + gvRow.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["CustomerID"] = gvRow.Cells[1].Text;
dt.Rows[dt.Rows.Count - 1]["ContactName"] = gvRow.Cells[2].Text;
dt.Rows[dt.Rows.Count - 1]["City"] = gvRow.Cells[3].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("CustomerID = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GetData();
gvAll.PageIndex = e.NewPageIndex;
BindGridone();
SetData();
}
protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
GetData();
SetData();
BindGridtwo();
}
private void BindGridtwo()
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
gvSelected.DataSource = dt;
gvSelected.DataBind();
}