1. 程式人生 > >前端界面操作DataTable數據表

前端界面操作DataTable數據表

erb 目錄 ons lin using 修改 connect from ldb


一、 知識點描述

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數據表