DataGridView填充、更新、删除(多行)Sql Express 2005数据库

  最近用到数据库,发现C#2005默认没有DataGrid,如果需要可以自己添加。并且提供了一个更新的DataGridView。但两者操作上还是有一些区别的。找了半天才找到一些处理数据的方法。具体做法就不说了,仅列出WinForm下DataGridView填充、更新、删除(多行)Sql Express 2005数据库代码。
        private void button1_Click(object sender, EventArgs e)
        {
            this.hostTableAdapter.Fill(this.hostScanDataSet.host);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.hostTableAdapter.Update(this.hostScanDataSet.host);
            MessageBox.Show("记录已保存!");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult dlResult = MessageBox.Show(this, "要删除这些记录吗?", "请确认",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            if (dlResult == DialogResult.Yes)
            {
                int j = dataGridView1.SelectedRows.Count;
                int[] l = new int[j];

                int i;
                for (i = 0; i < j; i++)
                {
                    l[i] = dataGridView1.SelectedRows[i].Index;
                }

                int k = 0;
                while (k < j)
                {
                    this.hostScanDataSet.host.Rows[l[k]].Delete();
                    k++;
                }
                this.hostTableAdapter.Update(this.hostScanDataSet.host);
            }
         }

 

posted on 2006-11-05 21:48  Mossan  阅读(36001)  评论(8编辑  收藏  举报