前端界面操作DataTable数据表
Posted yunqidick
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了前端界面操作DataTable数据表相关的知识,希望对你有一定的参考价值。
一、 知识点描述
DataGridView 取得或者修改当前单元格的内容:
当前单元格指的是 DataGridView 焦点所在的单元格,它可以通过 DataGridView 对象的 CurrentCell 属性取得。如果当前单元格不存在的时候,返回Nothing(C#是null)
// 取得当前单元格内容
Console.WriteLine(DataGridView1.CurrentCell.Value);
// 取得当前单元格的列 Index
Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex);
// 取得当前单元格的行 Index
Console.WriteLine(DataGridView1.CurrentCell.RowIndex);
另外,使用 DataGridView.CurrentCellAddress 属性(而不是直接访问单元格)来确定单元格所在的行:DataGridView.CurrentCellAddress.Y 和列: DataGridView.CurrentCellAddress.X 。这对于避免取消共享行的共享非常有用。
当前的单元格可以通过设定 DataGridView 对象的 CurrentCell 来改变。可以通过 CurrentCell 来设定
DataGridView 的激活单元格。将 CurrentCell 设为 Nothing(null) 可以取消激活的单元格。
二、 思维导图
三、 示例代码
1、 创建数据库连接SQLConnection
using (SqlConnection conn = new SqlConnection(DBHelper.connString))
2、 药品表填充进表一
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText =
"SELECT PrescriptionNo,PrescriptionName,PrescriptionPrice FROM Prescription WHERE PrescriptionNo NOT IN"
+ "(SELECT PrescriptionNo FROM SelectionPrescription WHERE PatientNo=‘patNo‘);";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
this.Prescription = new DataTable();
this.SelectionPrescription = new DataTable();
sqlConnection.Open();
sqlDataAdapter.Fill(this.Prescription);
3、个人药品表填充进表二
sqlCommand.CommandText =
"SELECT P.PrescriptionNo,P.PrescriptionName,P.PrescriptionPrice,SP.OrderPrescription"
+ " FROM Prescription AS P JOIN SelectionPrescription AS SP ON P.PrescriptionNo=SP.PrescriptionNo"
+ " WHERE SP.PatientNo=‘patNo‘;"; sqlDataAdapter.Fill(this.SelectionPrescription);
sqlConnection.Close();
4、 设置按钮进行数据传递操作
private void btn_Add_Click(object sender, EventArgs e)
{
if (this.dgv_Prescription.RowCount > 0)
{
DataRow
currentCourseRow = ((DataRowView)this.dgv_Prescription.CurrentRow.DataBoundItem).Row
, selectedCourseRow = this.SelectionPrescription.NewRow();
selectedCourseRow["PrescriptionNo"] = currentCourseRow["PrescriptionNo"];
selectedCourseRow["PrescriptionName"] = currentCourseRow["PrescriptionName"];
selectedCourseRow["PrescriptionPrice"] = currentCourseRow["PrescriptionPrice"];
this.SelectionPrescription.Rows.Add(selectedCourseRow);
currentCourseRow.Delete();
this.lbl_PrescriptionSum.Text =
"共" + this.SelectionPrescription.Compute("SUM(PrescriptionPrice)", "").ToString() + "元";
}
}
private void btn_Remove_Click(object sender, EventArgs e)
{
if (this.dgv_SelectionPrescription.RowCount > 0) {
DataRow selectedCourseRow =
((DataRowView)this.dgv_SelectionPrescription.CurrentRow.DataBoundItem).Row;
if (selectedCourseRow.RowState == DataRowState.Added)
{
string PrescriptionNo = selectedCourseRow["PrescriptionNo"].ToString();
DataRow deletedPrescriptionRow =
this.Prescription.Select("PrescriptionNo=‘" + PrescriptionNo + "‘", "", DataViewRowState.Deleted)[0];
deletedPrescriptionRow.RejectChanges();
this.SelectionPrescription.Rows.Remove(selectedCourseRow);
this.lbl_PrescriptionSum.Text =
"共" + this.SelectionPrescription.Compute("SUM(PrescriptionPrice)", "").ToString() + "元";
}
}
}
5、 每次数据传递后进行价格更新操作
if (this.dgv_Prescription.RowCount > 0)
{
DataRow
currentCourseRow = ((DataRowView)this.dgv_Prescription.CurrentRow.DataBoundItem).Row
, selectedCourseRow = this.SelectionPrescription.NewRow();
selectedCourseRow["PrescriptionNo"] = currentCourseRow["PrescriptionNo"];
selectedCourseRow["PrescriptionName"] = currentCourseRow["PrescriptionName"];
selectedCourseRow["PrescriptionPrice"] = currentCourseRow["PrescriptionPrice"];
this.SelectionPrescription.Rows.Add(selectedCourseRow);
currentCourseRow.Delete();
this.lbl_PrescriptionSum.Text =
"共" + this.SelectionPrescription.Compute("SUM(PrescriptionPrice)", "").ToString() + "元";
}
6、 提交选定后的药品目录
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
DBHelper.connString;
SqlCommand insertCommand = new SqlCommand();
insertCommand.Connection = sqlConnection;
insertCommand.CommandText =
"INSERT SelectionPrescription(PatientNo,PrescriptionNo,OrderPrescription)"
+ "VALUES(@PatientNo,@PrescriptionNo,@OrderPrescription);";
insertCommand.Parameters.AddWithValue("@PatientNo", "PatientNo");
insertCommand.Parameters.Add("@PrescriptionNo", SqlDbType.Char, 4, "PrescriptionNo");
insertCommand.Parameters.Add("@OrderPrescription", SqlDbType.Bit, 0, "OrderPrescription");
SqlCommand updateCommand = new SqlCommand();
updateCommand.Connection = sqlConnection;
updateCommand.CommandText =
"UPDATE SelectionPrescription"
+ " SET [email protected]"
+ " WHERE [email protected] AND [email protected];";
updateCommand.Parameters.AddWithValue("@StudentNo", "PatientNo");
updateCommand.Parameters.Add("@CourseNo", SqlDbType.Char, 4, "PrescriptionNo");
updateCommand.Parameters.Add("@OrderBook", SqlDbType.Bit, 0, "OrderPrescription");
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.InsertCommand = insertCommand;
sqlDataAdapter.UpdateCommand = updateCommand;
sqlConnection.Open();
int rowAffected = sqlDataAdapter.Update(this.SelectionPrescription);
sqlConnection.Close();
MessageBox.Show("插入" + rowAffected.ToString() + "行。");
四、 效果截图
以上是关于前端界面操作DataTable数据表的主要内容,如果未能解决你的问题,请参考以下文章