码迷,mamicode.com
首页 > 其他好文 > 详细

前端界面操作DataTable数据表

时间:2018-11-22 00:21:59      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:tostring   man   log   remove   nothing   ext   ndt   ntc   insert   


一、 知识点描述

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 OrderPrescription=@OrderPrescription"

+ " WHERE PatientNo=@StudentNo AND PrescriptionNo=@PrescriptionNo;";

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数据表

标签:tostring   man   log   remove   nothing   ext   ndt   ntc   insert   

原文地址:https://www.cnblogs.com/YunQiDick/p/9998349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!