基于ArcGIS10.0和Oracle10g的空間數(shù)據(jù)管理平臺十八(C#開發(fā))-數(shù)據(jù)字典編輯
我的新浪微博:http://weibo.com/freshairbrucewoo。
歡迎大家相互交流,共同提高技術。
今天繼續(xù)接著前面介紹的開發(fā)通用空間數(shù)據(jù)管理平臺這個項目進行講解,今天介紹的內(nèi)容比較簡單,就是通過界面來實現(xiàn)數(shù)據(jù)字典的編輯。至于數(shù)據(jù)字典的概念大家可以網(wǎng)上查找,以前我有一篇博客專門針對這個項目中用到的專業(yè)術語進行過講解,也可以去查看一下。
介紹的思路還是按照當時自己在實現(xiàn)這個功能的思路,基本上還原當時怎樣一步一步把這個相對簡單的功能實現(xiàn)的。
1.定義用到的成員變量。
- private OracleCommandBuilder builder;//命令構建
- private OracleDataAdapter da;//數(shù)據(jù)適配器
- private DataSet ds;//數(shù)據(jù)集
- private string selectedNodeText;//保存選擇樹形節(jié)點的文本內(nèi)容
- private string tableName;//表的名稱
- protected OracleConnection Connection;//連接數(shù)據(jù)庫
- private bool isChanged = false;//控件中的內(nèi)容是否改變
2.在form的裝載函數(shù)中初始化界面的相關控件,讓這些控件都處于一個正常的顯示狀態(tài)并且為后面的操作做好準備。
- private void DataDictionaryEdit_Load(object sender, EventArgs e)
- {
- //加載相應的數(shù)據(jù)字典表到Tree
- Connection = new OracleConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
- Node tn = new Node();
- tn.Text = "數(shù)據(jù)字典表";
- advTree1.Nodes.Add(tn);
- Node n1 = new Node();
- n1.Text = "數(shù)據(jù)分類表";
- Node n2 = new Node();
- n2.Text = "要素類別表";
- Node n3 = new Node();
- n3.Text = "圖層表";
- Node n4 = new Node();
- n4.Text = "字段定義表";
- tn.Nodes.Add(n1);
- tn.Nodes.Add(n2);
- tn.Nodes.Add(n3);
- tn.Nodes.Add(n4);
- }
3.通過樹形控件選擇相應的節(jié)點以后,通過樹形節(jié)點的文字(代表表名稱)選擇一個表的內(nèi)容加載到datagridView控件中,并且實現(xiàn)分頁顯示功能。
- //選擇相應的表以后加載數(shù)據(jù)到DataGridView中
- private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
- {
- Node tn = new Node();
- tn = e.Node;
- selectedNodeText = tn.Text;
- switch (tn.Level)
- {
- case 1:
- labelX1.Text = "";
- labelX1.Text = "當前數(shù)據(jù)表: " + tn.Text;
- if (Connection.State != ConnectionState.Open)
- {
- Connection.Open();
- }
- if (tn.Text == "數(shù)據(jù)分類表")
- {
- tableName = "jcsjk_category";
- }
- else if (tn.Text == "要素類別表")
- {
- tableName = "jcsjk_element";
- }
- else if (tn.Text == "圖層表")
- {
- tableName = "jcsjk_layer";
- }
- else if (tn.Text == "字段定義表")
- {
- tableName = "jcsjk_fielddefine";
- }
- string sql = "select * from " + tableName;
- da = new OracleDataAdapter(sql, Connection);
- builder = new OracleCommandBuilder(da);
- ds = new DataSet();
- da.Fill(ds, tableName);
- dataGridViewX1.DataSource = ds.Tables[0];
- int intMod, dgr;
- //先讓垂直滾動條消失
- dataGridViewX1.ScrollBars = ScrollBars.Horizontal;
- //取出DGV的行數(shù),為什么要減一是因為它總是多出一行給你編輯的所以那行也占用一行的空間
- dgr = dataGridViewX1.RowCount - 1;
- //進行取模
- if (dgr % 10 == 0)
- {
- intMod = 0;
- }
- else
- {
- intMod = 1;
- }
- //主要時這個for循環(huán)將表一共分為幾頁添加到comboBox
- comboBoxEx1.Items.Clear();
- for (int i = 1; i <= dgr / 10 + intMod; i++)
- {
- comboBoxEx1.Items.Add("第" + i + "頁");
- }
- //默認選中第一個
- if (comboBoxEx1.Items.Count > 0)
- {
- comboBoxEx1.SelectedIndex = 0;
- }
- break;
- default:
- break;
- }
- }
4.在控件增加和刪除一條記錄,此時還沒有更新到數(shù)據(jù)庫,需要保持以后才真正進入數(shù)據(jù)庫中,后面一個功能實現(xiàn)。
- //刪除一條記錄
- private void delBtn_Click(object sender, EventArgs e)
- {
- if (!dataGridViewX1.AllowUserToDeleteRows)
- {
- dataGridViewX1.AllowUserToDeleteRows = true;
- }
- if (dataGridViewX1.CurrentRow.Index < 0)
- {
- MessageBox.Show("請選擇需要刪除的一行");
- return;
- }
- isChanged = true;
- ds.Tables[0].Rows[dataGridViewX1.CurrentRow.Index].Delete();
- }
- //增加一條記錄
- private void addBtn_Click(object sender, EventArgs e)
- {
- if (!dataGridViewX1.AllowUserToAddRows)
- {
- dataGridViewX1.AllowUserToAddRows = true;
- }
- if (dataGridViewX1.ReadOnly)
- {
- dataGridViewX1.ReadOnly = false;
- }
- isChanged = true;
- dataGridViewX1.FirstDisplayedScrollingRowIndex =
- dataGridViewX1.Rows[dataGridViewX1.RowCount - 1].Index;
- }
5.把控件中改動、增加或刪除的內(nèi)容更新到數(shù)據(jù)庫中去,可以同時對控件中的內(nèi)容進行增加、刪除和修改,然后一次性更新到數(shù)據(jù)庫,但是每次只能操作一個數(shù)據(jù)字典表。
- //修改后的數(shù)據(jù)保存到數(shù)據(jù)庫中去
- private void saveBtn_Click(object sender, EventArgs e)
- {
- int result = 0;
- if (isChanged)
- {
- try
- {
- result = da.Update(ds, tableName);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- isChanged = false;
- dataGridViewX1.ReadOnly = true;
- dataGridViewX1.AllowUserToAddRows = false;
- dataGridViewX1.AllowUserToDeleteRows = false;
- if (result > 0)
- {
- LogHelp.writeLog(FrmMain.username, "數(shù)據(jù)字典編輯",
- "數(shù)據(jù)字典" + selectedNodeText + "編輯成功");
- MessageBox.Show("更新數(shù)據(jù)庫成功");
- }
- else
- {
- LogHelp.writeLog(FrmMain.username, "數(shù)據(jù)字典編輯",
- "數(shù)據(jù)字典" + selectedNodeText + "編輯失敗");
- MessageBox.Show("數(shù)據(jù)庫沒有更新");
- }
- }
6.其他輔助小功能。
- //使能編輯
- private void editBtn_Click(object sender, EventArgs e)
- {
- if (dataGridViewX1.ReadOnly)
- {
- dataGridViewX1.ReadOnly = false;
- }
- }
- //檢查數(shù)據(jù)錄入格式
- private void dataGridViewX1_DataError(object sender, DataGridViewDataErrorEventArgs e)
- {
- labelX2.Text = "數(shù)據(jù)錄入格式不正確";
- }
- //記錄值是否發(fā)生改變
- private void dataGridViewX1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
- {
- isChanged = true;
- }
- //DataGridView全部可見
- private void showAllBtn_Click(object sender, EventArgs e)
- {
- dataGridViewX1.ScrollBars = ScrollBars.Both;
- try
- {
- dataGridViewX1.FirstDisplayedScrollingRowIndex = 0;
- }
- catch (ArgumentOutOfRangeException)
- {
- }
- }
- //顯示第幾頁數(shù)據(jù)
- private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
- {
- try
- {
- dataGridViewX1.FirstDisplayedScrollingRowIndex = comboBoxEx1.SelectedIndex * 10;
- }
- catch (ArgumentOutOfRangeException)
- {
- }
- }
其他功能主要包括:定位顯示第幾頁的數(shù)據(jù)、使能控件可以編輯、輸入控件值的有效性檢查等。
7、總結。
這里所謂的數(shù)據(jù)字典編輯其實也就是針對幾個表進行操作,和普通的數(shù)據(jù)庫表操作完全一樣,只是根據(jù)功能記錄的數(shù)據(jù)類型不一樣而已,數(shù)據(jù)字典可能聽起來更加專業(yè)一些,它的作用更加突出明顯一些。
浙公網(wǎng)安備 33010602011771號