[英]Using a checkbox input in Gridview and inserting is value in to SQL DB
我正在嘗試在 gridview 中使用復選框控件。 基本上,用戶將輸入一個項目描述、所用項目的數量,以及它是否已經“售出”到庫存中。 選中該復選框將表明該項目已售出。 未選中它可以是 null 或否。 我可以將數據庫中的值存儲為 1/2、真/假、是/否。 沒關系。
我曾嘗試在我的數據庫中使用位類型、int 和 vchar。 並嘗試了多種方法來獲取 C# 中的檢查值,但似乎無法使其正常工作。
我已經搜索了所有內容,並找到了有關如何使用它來保存或刪除多行的示例。 但我需要它在我的數據庫中實際存儲一個值。
這是我從其他帖子中收集的代碼,幾乎可以正常工作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox itemDesc = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtItemDesc");
TextBox quantity = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
CheckBox sold = (CheckBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("alreadySold");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = itemDesc.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = quantity.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = sold.Checked.ToString();
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox itemDesc = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtItemDesc");
TextBox quantity = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
CheckBox sold = (CheckBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("alreadySold");
itemDesc.Text = dt.Rows[i]["Column1"].ToString();
quantity.Text = dt.Rows[i]["Column2"].ToString();
sold.Checked = dt.Rows[i]["Column3"].ToString().ToUpperInvariant() == "TRUE";
rowIndex++;
}
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void Save(object sender, System.EventArgs e)
{
if (ViewState["CurrentTable"] != null)
{
DataTable dt = new DataTable();
dt = ViewState["CurrentTable"] as DataTable;
if (dt.Rows.Count > 0)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
SqlCommand cmd = null;
foreach (GridViewRow row in Gridview1.Rows)
{
cmd = new SqlCommand("INSERT INTO SampleTest(ItemDesc,Quantity,Sold) VALUES(@Column1,@Column2,@Column3)", con);
string itemDesc = (row.FindControl("txtItemDesc") as TextBox).Text;
string quantity = (row.FindControl("txtQuantity") as TextBox).Text;
CheckBox sold = (row.FindControl("alreadySold") as CheckBox);
if (sold == null)
{
sold.Checked = false;
}
else
{
sold.Checked = true;
}
cmd.Parameters.AddWithValue("@Column1", itemDesc);
cmd.Parameters.AddWithValue("@Column2", quantity);
cmd.Parameters.AddWithValue("@Column3", sold);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
//cmd = new SqlCommand("SELECT Column1,Column2,Column3 FROM SampleTest", con);
//SqlDataAdapter da = new SqlDataAdapter(cmd);
//DataTable dt1 = new DataTable();
//da.Fill(dt1);
//this.gvSample.DataSource = dt1;
//this.gvSample.DataBind();
}
}
}
}
}
}
在您的保存中,而不是CheckBox sold
嘗試類似:
bool sold = (row.FindControl("alreadySold") as CheckBox)?.Checked?? false;
所以對上面的一點解釋:
現在,如果您的數據庫在該列中允許 NULL 並且您對此感到滿意,您還可以執行以下操作:
bool? sold = (row.FindControl("alreadySold") as CheckBox)?.Checked;
^ 布爾? 是一種所謂的 Nullable 值類型。 值類型不能是 null,但可空值類型環繞一個值類型並允許它是 null。
我是個白痴。 感謝 BOB 解釋 null 值和復選框控件發生了什么。 它真的幫助我了解發生了什么。 其他事情不起作用的原因是因為我在 html 頁面上的復選框控件上有一個 type-o。 一旦我修復了一切正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.