-
04-11-2015, 10:56 AM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Làm các nút di chuyển First,Prev,Next và Last trên datagirdview
Đầu tiên các bạn tạo một table trong SQL như sau:
Create Table GiaoVien
(
MaGV Char(10) Not Null Primary Key,
HoTen Nvarchar(50) Not Null,
DiaChi Nvarchar(100) Not Null,
DienThoai Char(11) Null,
ChuyenMon Nvarchar(100)
)
Go
Rồi bây giờ các bạn tạo một projiect trên VS đặt đại một tên gì cũng được.
bây giờ bạn vào trong Form1 và kéo thả những Control như sau:
+Label 3 cái: và đặt tên:
+ 3 Textbox và lần lượt đặt name như sau: txtID,txtName,txtFone
+ tiếp theo bạn kéo thả 1 datagirdview mặc định Name la datagirdview1
+tiếp theo bạn kéo thả 4 button va lần lượt đặt name: btnFirst, btnPrevious,btnNext,btnLast
http://diendan.congdongcviet.com/attachment.php?attachmentid=40391&d=1446626630
Xong bây giờ bạn vào code như sau:
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.SqlClient;
namespace DemoNavigationUsingBindingSource
{
public partial class Form1 : Form
{
SqlConnection conn;
SqlDataAdapter adapter;
DataTable datatable = new DataTable();
BindingSource bindingsource;
public Form1()
{
InitializeComponent();
}
private void DataLoad()
{
conn = new SqlConnection("Data Source=(Local);Initial Catalog=QLGIANGDAYGV;Integrated Security=True");//các bạn tự đặt Initial Catalog= Tên CSDL các bạn tạo trên SQL nha.
conn.Open();
SqlCommand cmd = new SqlCommand("select * from tblGiaoVien",conn);
datatable = new DataTable();
datatable.Load(cmd.ExecuteReader());
conn.Close();
bindingsource = new BindingSource();
bindingsource.DataSource = datatable;
dataGridView1.DataSource = bindingsource;
}
private void ShowRecord()
{
txtID.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
txtName.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
txtFone.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
DataLoad();
ShowRecord();
}
private void btnFirst_Click(object sender, EventArgs e)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[0].Selected = true;
bindingsource.DataSource = datatable;
dataGridView1.DataSource = bindingsource;
bindingsource.MoveFirst();
ShowRecord();
}
private void btnNext_Click(object sender, EventArgs e)
{
try
{
int IndexNow = dataGridView1.CurrentRow.Index;
if (dataGridView1.Rows.Count > IndexNow)
{
dataGridView1.Rows[IndexNow + 1].Selected = true;
}
bindingsource.DataSource = datatable;
dataGridView1.DataSource = bindingsource;
bindingsource.MoveNext();
}
catch (Exception)
{
}
ShowRecord();
}
private void btnPrevious_Click(object sender, EventArgs e)
{
try
{
int IndexNow = dataGridView1.CurrentRow.Index;
if (dataGridView1.Rows.Count > IndexNow)
{
dataGridView1.Rows[IndexNow - 1].Selected = true;
}
bindingsource.DataSource = datatable;
dataGridView1.DataSource = bindingsource;
bindingsource.MovePrevious();
}
catch (Exception)
{
}
ShowRecord();
}
private void btnLast_Click(object sender, EventArgs e)
{
dataGridView1.ClearSelection();
int so = dataGridView1.Rows.Count - 1;
//MessageBox.Show(so.ToString());
dataGridView1.Rows[so - 1].Selected = true;
bindingsource.DataSource = datatable;
dataGridView1.DataSource = bindingsource;
bindingsource.MoveLast();
ShowRecord();
}
private void dataGridView1_Click(object sender, EventArgs e)
{
ShowRecord();
}
}
}
Vậy là xong bây giờ các bạn có thể di chuyển theo ý muốn bằng button mà mình tự tạo ra rồi đấy.
Nếu thấy bài viết hay thì thank giùm mình cái nhaView more random threads:
- Hướng dẫn tách dữ liệu từ ListBox sang TextBox
- Xin giúp đỡ về thuật toán làm tkb
- Tạo chương trình chỉ chạy 1 instance 1 lúc trong lập trình C#
- Giúp mình làm REPORT trong Asp.net với :(((((
- Mạng ✲ Mobile ✲ 5G ✥ có ✥ thể ۞ tới ✥ sớm ๑ hơn ❣ bạn ✚ nghĩ
- Game Đoán chữ + Chiếc nón kì diệu
- tạo webservice đơn giản giải phương trình bậc hai
- Xử lý Bitmap, Color trong lập trình C#
- Hướng dẫn lấy địa chỉ IP và các thông tin liên quan
- Chuyển đổi các kiểu dữ liệu cơ bản sang mảng Byte trong C#.
Pallet nhựa Lâm Đồng giải pháp hoàn hảo cho nhà kho Pallet nhựa Lâm Đồng là một giải pháp đa năng và hiệu quả trong việc vận chuyển và lưu trữ hàng hóa. Trong đó, pallet nhựa Lâm Đồng không chỉ đáp...
Pallet nhựa Lâm Đồng giải pháp...