DataGridView控件怎么在C#項(xiàng)目中使用?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

1) 創(chuàng)建課程信息表創(chuàng)建課程信息表的 SQL 語句如下。
use test; create table course ( id int primary key identity(1,1), name varchar(20), credit numeric(3,1), remark varchar(50) );
向表中添加數(shù)據(jù)的語句如下。
insert into course (name, credit, remark) values ('計(jì)算機(jī)基石 ' , 2, '無');
insert into course (name, credit, remark) values ('C# 程序開發(fā)', 2.5 , '機(jī)房授課');
insert into course (name, credit, remark) values ('數(shù)據(jù)庫(kù)原理',1,'無');
insert into course (name, credit, remark) values ('體育',1,'無');
insert into course (name, credit, remark) values ('職業(yè)素養(yǎng)培訓(xùn)',0.5,'無');在 SQL Server 中執(zhí)行上述 SQL 語句即可完成課程信息表(course)的創(chuàng)建和數(shù)據(jù)的添加。
2) 課程信息管理界面的設(shè)計(jì)在課程信息管理界面中提供了 DataGridView 控件用于顯示課程信息,并提供了根據(jù)課程名稱查找課程信息、修改以及刪除的功能。
具體的界面設(shè)計(jì)如下圖所示。

3) 在加載窗體時(shí)顯示所有課程信息本例中使用編寫代碼的方式實(shí)現(xiàn) DataGridView 控件的數(shù)據(jù)綁定,并在窗體的加載事件中加入數(shù)據(jù)綁定的代碼。
由于查詢所有課程信息的代碼將在后面的修改和刪除功能中重復(fù)使用,所以單獨(dú)定義一個(gè)方法來實(shí)現(xiàn)查詢所有課程信息。代碼如下。
//窗體加載事件
private void DataGridViewForm_Load(object sender, EventArgs e)
{
//調(diào)用查詢?nèi)空n程的方法
QueryAllCourse();
}
//查詢?nèi)空n程
private void QueryAllCourse()
{
//數(shù)據(jù)庫(kù)連接串
string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root";
//創(chuàng)建SqlConnection的實(shí)例
SqlConnection conn = null;
try
{
conn = new SqlConnection(connStr);
//打開數(shù)據(jù)庫(kù)
conn.Open();
string sql = "select * from course";
//創(chuàng)建SqlDataAdapter類的對(duì)象
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
//創(chuàng)建DataSet類的對(duì)象
DataSet ds = new DataSet();
//使用SqlDataAdapter對(duì)象sda將查新結(jié)果填充到DataSet對(duì)象ds中
sda.Fill(ds);
//設(shè)置表格控件的DataSource屬性
dataGridView1.DataSource = ds.Tables[0];
//設(shè)置數(shù)據(jù)表格上顯示的列標(biāo)題
dataGridView1.Columns[0].HeaderText = "編號(hào)";
dataGridView1.Columns[1].HeaderText = "課程名稱";
dataGridView1.Columns[2].HeaderText = "學(xué)分";
dataGridView1.Columns[3].HeaderText = "備注";
//設(shè)置數(shù)據(jù)表格為只讀
dataGridView1.ReadOnly = true;
//不允許添加行
dataGridView1.AllowUserToAddRows = false;
//背景為白色
dataGridView1.BackgroundColor = Color.White;
//只允許選中單行
dataGridView1.MultiSelect = false;
//整行選中
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
catch (Exception ex)
{
MessageBox.Show("查詢錯(cuò)誤!" + ex.Message);
}
finally
{
if (conn != null)
{
//關(guān)閉數(shù)據(jù)庫(kù)連接
conn.Close();
}
}
}運(yùn)行該窗體,效果如下圖所示。

4) 完成課程名稱的模糊查詢?cè)凇安樵儭卑粹o的單擊事件中加入根據(jù)課程名稱模糊查詢的代碼,具體如下。
//查詢按鈕單擊事件
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
//數(shù)據(jù)庫(kù)連接串
string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root";
//創(chuàng)建SqlConnection的實(shí)例
SqlConnection conn = null;
try
{
conn = new SqlConnection(connStr);
//打開數(shù)據(jù)庫(kù)
conn.Open();
string sql = "select * from course where name like '%{0}%'";
//填充占位符
sql = string.Format(sql, textBox1.Text);
//創(chuàng)建SqlDataAdapter類的對(duì)象
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
//創(chuàng)建DataSet類的對(duì)象
DataSet ds = new DataSet();
//使用SqlDataAdapter對(duì)象sda將查新結(jié)果填充到DataSet對(duì)象ds中
sda.Fill(ds);
//設(shè)置表格控件的DataSource屬性
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show("出現(xiàn)錯(cuò)誤!" + ex.Message);
}
finally
{
if (conn != null)
{
//關(guān)閉數(shù)據(jù)庫(kù)連接
conn.Close();
}
}
}
}運(yùn)行該窗體,查詢效果如下圖所示。

從上面的運(yùn)行效果可以看出,在文本框中輸入“計(jì)算機(jī)”,則可以實(shí)現(xiàn)查詢所有課程 名稱中含有“計(jì)算機(jī)”字樣的課程信息。
5) 實(shí)現(xiàn)修改功能在 DataGridView 控件中選中一條課程信息,單擊“修改”按鈕,彈出修改課程信息界面并在該界面中顯示要修改的信息,修改界面的設(shè)計(jì)如下圖所示。

選中 DataGridView 控件的一條課程信息,單擊“修改”按鈕。
“修改”按鈕的單擊事件中的代碼如下。
//修改課程信息
private void button2_Click(object sender, EventArgs e)
{
//獲取DataGridView控件中的值
//獲取課程編號(hào)
string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
//獲取課程名稱
string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
//獲取課程名稱
string credit = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
//獲取課程名稱
string remark = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
//創(chuàng)建updateForm類的對(duì)象,并將課程信息傳遞給修改界面
updateForm updateform = new updateForm(id, name, credit, remark);
//彈出修改信息窗口
DialogResult dr = updateForm.ShowDialog();
//判斷是否單擊確定按鈕
if (dr == DialogResult.OK)
{
//調(diào)用查詢?nèi)空n程方法
QueryAllCourse();
}
}修改界面 (UpdateForm) 中的代碼如下。
//帶參數(shù)的構(gòu)造方法
public updateForm(string id,string name,string credit,string remark)
{
InitializeComponent();
textBox1.Text = id;
textBox2.Text = name;
textBox3.Text = credit;
textBox4.Text = remark;
}
//確認(rèn)按鈕單擊事件
private void button1_Click(object sender, EventArgs e)
{
//數(shù)據(jù)庫(kù)連接串
string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root";
//創(chuàng)建SqlConnection的實(shí)例
SqlConnection conn = null;
try
{
conn = new SqlConnection(connStr);
//打開數(shù)據(jù)庫(kù)
conn.Open();
string sql = "update course set name='{0}',credit='{1}',remark='{2}' where id='{3}'";
//填充占位符
sql = string.Format(sql, textBox2.Text, textBox3.Text, textBox4.Text, textBox1.Text);
//創(chuàng)建SqlCommand類的對(duì)象
SqlCommand cmd = new SqlCommand(sql, conn);
//執(zhí)行修改操作的SQL
cmd.ExecuteNonQuery();
//彈出成功提示
MessageBox.Show("修改成功!");
//設(shè)置當(dāng)前窗體DislogResult結(jié)果為OK
this.DialogResult = DialogResult.OK;
//關(guān)閉窗體
this.Close();
}
catch (Exception ex)
{
MessageBox.Show("修改失敗!" + ex.Message);
}
finally
{
if (conn != null)
{
//關(guān)閉數(shù)據(jù)庫(kù)連接
conn.Close();
}
}
}
//取消按鈕單擊事件
private void button2_Click(object sender, EventArgs e)
{
//關(guān)閉窗體
this.Close();
}修改操作的運(yùn)行效果如下圖所示。

6) 實(shí)現(xiàn)刪除功能為“刪除”按鈕添加單擊事件,將選中的課程信息刪除并刷新界面中查詢出來的數(shù)據(jù)。實(shí)現(xiàn)的代碼如下。
//刪除按鈕的單擊事件
private void button3_Click(object sender, EventArgs e)
{
//獲取DataGridView控件中選中行的編號(hào)列的值
int id = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
//數(shù)據(jù)庫(kù)連接串
string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root";
//創(chuàng)建SqlConnection的實(shí)例
SqlConnection conn = null;
try
{
conn = new SqlConnection(connStr);
//打開數(shù)據(jù)庫(kù)
conn.Open();
string sql = "delect from course where id='{0}'";
//填充占位符
sql = string.Format(sql, id);
//創(chuàng)建SqlCommand類的對(duì)象
SqlCommand cmd = new SqlCommand(sql, conn);
//執(zhí)行SQL語句
cmd.ExecuteNonQuery();
//彈出消息提示刪除成功
MessageBox.Show("刪除成功!");
//調(diào)用查詢?nèi)康姆椒ǎ⑿翫ataGridView控件中的數(shù)據(jù)
QueryAllCourse();
}
catch (Exception ex)
{
MessageBox.Show("刪除失敗!" + ex.Message);
}
finally
{
if (conn != null)
{
//關(guān)閉數(shù)據(jù)庫(kù)連接
conn.Close();
}
}
}刪除操作的運(yùn)行效果如下圖所示。

單擊刪除消息框中的“確定”按鈕,'即可刷新 DataGridView 控件中的數(shù)據(jù)。
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
網(wǎng)站標(biāo)題:DataGridView控件怎么在C#項(xiàng)目中使用-創(chuàng)新互聯(lián)
文章源于:http://www.chinadenli.net/article46/djhehg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、定制網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容