c#项目实战_如何在C#中创建高级记录导航系统 datagridview写入数据

c#项目实战_如何在C#中创建高级记录导航系统 datagridview写入数据

本教程是我们上一个主题“如何在C#中创建简单的记录导航”的续篇。但此时,我们将修改代码并添加更多附加控件,如第一条记录、最后一条记录和记录数。如下所示。

为了开始构建这个应用程序,让我们首先打开我们最后一个名为“student_info”的项目。然后添加按钮和标签。接下来,按照上图所示进行排列和设计。

这次我们将在四个按钮上添加功能,比如First、Previous、Next和Last。首先,双击“第一条记录”按钮。然后添加以下代码:

private void btnfirst_Click(object sender, EventArgs e)

{

//check if the current record is not equal to zero

if (curRecord != 0)

//set the current record to zero

curRecord = 0;

//call the Navaigate sub procedure

Navigate();

//set the record value

lblrecord.Text = curRecord + 1 + " of " + totRecord;

}

然后是“上一条记录”按钮。添加以下代码:

private void btnprev_Click(object sender, EventArgs e)

{

//check if the current record is greater than zero

if (curRecord > 0)

{

//then decrement the current record by one

curRecord = curRecord - 1;

//call the Navaigate sub procedure

Navigate();

//set the record value

lblrecord.Text = curRecord + 1 + " of " + totRecord;

}

}

“下一条记录”按钮。添加以下代码:

private void btnnext_Click(object sender, EventArgs e)

{

//check if the current record is not equal to the total Record minus by one

if (curRecord != totRecord - 1)

{

//increment the current record by one

curRecord = curRecord + 1;

//call the Navaigate sub procedure

Navigate();

//set the record value

lblrecord.Text = curRecord + 1 + " of " + totRecord;

}

}

最后是“最后记录”按钮。下面是代码:

private void btnlast_Click(object sender, EventArgs e)

{

//check if the current Record is not Equal to the total record

if (curRecord != totRecord)

//set the current record equal to total record

curRecord = totRecord - 1;

//call the Navaigate sub procedure

Navigate();

//set the record value

lblrecord.Text = curRecord + 1 + " of " + totRecord;

}

此时,您可以按“F5”或“开始”按钮来测试应用程序。

下面是这个应用程序使用的所有代码。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

namespace student_info

{

public partial class Form1 : Form

{

//declare new variable named dt as New Datatable

DataTable dt = new DataTable();

//this line of code used to connect to the server and locate the database (usermgt.mdb)

static string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + "/studentdb.mdb";

OleDbConnection conn = new OleDbConnection(connection);

int curRecord = 0;

int totRecord = 0;

public Form1()

{

InitializeComponent();

}

private void btnload_Click(object sender, EventArgs e)

{

//create a new datatable

dt = new DataTable();

//create our SQL SELECT statement

string sql = "Select * from tblstudent";

//then we execute the SQL statement against the Connection using OleDBDataAdapter

OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);

//we fill the result to dt which declared above as datatable

da.Fill(dt);

//set the curRecord to zero

curRecord = 0;

//get the total number of record available in the database and stored to totRecord Variable

totRecord = dt.Rows.Count;

//then we populate the datagridview by specifying the datasource equal to dt

dataGridView1.DataSource = dt;

}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

{

//it checks if the row index of the cell is greater than or equal to zero

if (e.RowIndex >= 0)

{

//gets a collection that contains all the rows

DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];

//populate the textbox from specific value of the coordinates of column and row.

txtid.Text = row.Cells[0].Value.ToString();

txtfname.Text = row.Cells[1].Value.ToString();

txtlname.Text = row.Cells[2].Value.ToString();

comboBox1.Text = row.Cells[3].Value.ToString();

txtgender.Text = row.Cells[4].Value.ToString();

txtaddress.Text = row.Cells[5].Value.ToString();

}

}

private void Form1_Load(object sender, EventArgs e)

{

loadDatatoCombobox();

}

private void loadDatatoCombobox()

{

//create a new datatable

DataTable table = new DataTable();

//create our SQL SELECT statement

string sql = "Select course from tblstudent";

try

{

conn.Open();

//then we execute the SQL statement against the Connection using OleDBDataAdapter

OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);

//we fill the result to dt which declared above as datatable

da.Fill(table);

//we add new entry to our datatable manually

//becuase Select Course is not Available in the Database

table.Rows.Add("Select Course");

//set the combobox datasource

comboBox1.DataSource = table;

//choose the specific field to display

comboBox1.DisplayMember = "course";

comboBox1.ValueMember = "course";

//set default selected value

comboBox1.SelectedValue = "Select Course";

}

catch (Exception ex)

{

//this will display some error message if something

//went wrong to our code above during execution

MessageBox.Show(ex.ToString());

}

}

private void Navigate()

{

txtid.Text = dt.Rows[curRecord][0].ToString();

txtfname.Text = dt.Rows[curRecord][1].ToString();

txtlname.Text = dt.Rows[curRecord][2].ToString();

comboBox1.Text = dt.Rows[curRecord][3].ToString();

txtgender.Text = dt.Rows[curRecord][4].ToString();

txtaddress.Text = dt.Rows[curRecord][5].ToString();

}

private void btnprev_Click(object sender, EventArgs e)

{

//check if the current record is greater than zero

if (curRecord > 0)

{

//then decrement the current record by one

curRecord = curRecord - 1;

//call the Navaigate sub procedure

Navigate();

//set the record value

lblrecord.Text = curRecord + 1 + " of " + totRecord;

}

}

private void btnnext_Click(object sender, EventArgs e)

{

//check if the current record is not equal to the total Record minus by one

if (curRecord != totRecord - 1)

{

//increment the current record by one

curRecord = curRecord + 1;

//call the Navaigate sub procedure

Navigate();

//set the record value

lblrecord.Text = curRecord + 1 + " of " + totRecord;

}

}

private void btnfirst_Click(object sender, EventArgs e)

{

//check if the current record is not equal to zero

if (curRecord != 0)

//set the current record to zero

curRecord = 0;

//call the Navaigate sub procedure

Navigate();

//set the record value

lblrecord.Text = curRecord + 1 + " of " + totRecord;

}

private void btnlast_Click(object sender, EventArgs e)

{

//check if the current Record is not Equal to the total record

if (curRecord != totRecord)

//set the current record equal to total record

curRecord = totRecord - 1;

//call the Navaigate sub procedure

Navigate();

//set the record value

lblrecord.Text = curRecord + 1 + " of " + totRecord;

}

}

}

以上是 c#项目实战_如何在C#中创建高级记录导航系统 datagridview写入数据 的全部内容, 来源链接: utcz.com/wiki/669443.html

回到顶部