ขอโค้ดปุ่ม Search ค้นหา VB 2010 MySql Connector ผ่าน DataGridView
ทำไมไม่ใช้ MySql.Data NameSpace ครับ
Code (VB.NET)
Imports System.Data
Imports MySql.Data.MySqlClient
Code (VB.NET)
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
strKeyWord = Me.txtSearch.Text
PopulateData()
End Sub
Private Sub PopulateData()
'*** BindData From Database **'
Dim objConn As MySqlConnection
Dim dtAdapter As MySqlDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
objConn = New MySqlConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM customer WHERE 1=1 "
If strKeyWord <> "" Then
strSQL = strSQL & " AND Name Like '%" & strKeyWord & "%' "
End If
dtAdapter = New MySqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Me.myDgv.DataSource = dt
'******************************'
Me.myDgv.Columns.Clear()
With myDgv.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(songsDataGridView.Font, FontStyle.Bold)
End With
Dim ColumnTextBox As DataGridViewTextBoxColumn
'*** Column (Select) ***'
Dim ColumnCheckBox As New DataGridViewCheckBoxColumn()
ColumnCheckBox.Width = 30
ColumnCheckBox.DataPropertyName = "Select"
Me.myDgv.Columns.Add(ColumnCheckBox)
'*** Checkbox in Header ***'
Dim ckBox As New CheckBox()
Dim rect As Rectangle = myDgv.GetCellDisplayRectangle(0, -1, True)
ckBox.Size = New Size(18, 18)
rect.X = rect.Location.X + (rect.Width / 4)
ckBox.Location = rect.Location
AddHandler ckBox.CheckedChanged, New EventHandler(AddressOf ckBox_CheckedChanged)
Me.myDgv.Controls.Add(ckBox)
myDgv.Columns(0).Frozen = False
'*** Column (CustomerID) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "CustomerID"
ColumnTextBox.HeaderText = "CustomerID"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (Name) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Name"
ColumnTextBox.HeaderText = "Name"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (Email) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Email"
ColumnTextBox.HeaderText = "Email"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (CountryCode) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "CountryCode"
ColumnTextBox.HeaderText = "CountryCode"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (Budget) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Budget"
ColumnTextBox.HeaderText = "Budget"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
'*** Column (Used) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Used"
ColumnTextBox.HeaderText = "Used"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.myDgv.Columns.Add(ColumnTextBox)
End Sub
Go to : VB.NET/C# MySQL (ADO.NET) พื้นฐานการ เขียนโปรแกรม ADO.NET เชื่อมต่อกับฐานข้อมูล MySQL
Date :
2012-03-26 13:28:23
By :
webmaster
for C#
Code (C#)
using System.Data;
using MySql.Data.MySqlClient;
Code (C#)
private void btnSearch_Click(System.Object sender, System.EventArgs e)
{
strKeyWord = this.txtSearch.Text;
PopulateData();
}
private void PopulateData()
{
//*** BindData From Database **'
MySqlConnection objConn = default(MySqlConnection);
MySqlDataAdapter dtAdapter = default(MySqlDataAdapter);
DataTable dt = new DataTable();
string strConnString = null;
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false";
objConn = new MySqlConnection(strConnString);
objConn.Open();
string strSQL = null;
strSQL = "SELECT * FROM customer WHERE 1=1 ";
if (!string.IsNullOrEmpty(strKeyWord)) {
strSQL = strSQL + " AND Name Like '%" + strKeyWord + "%' ";
}
dtAdapter = new MySqlDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
dtAdapter = null;
objConn.Close();
objConn = null;
this.myDgv.DataSource = dt;
//******************************'
this.myDgv.Columns.Clear();
var _with1 = myDgv.ColumnHeadersDefaultCellStyle;
_with1.BackColor = Color.Navy;
_with1.ForeColor = Color.White;
_with1.Font = new Font(songsDataGridView.Font, FontStyle.Bold);
DataGridViewTextBoxColumn ColumnTextBox = default(DataGridViewTextBoxColumn);
//*** Column (Select) ***'
DataGridViewCheckBoxColumn ColumnCheckBox = new DataGridViewCheckBoxColumn();
ColumnCheckBox.Width = 30;
ColumnCheckBox.DataPropertyName = "Select";
this.myDgv.Columns.Add(ColumnCheckBox);
//*** Checkbox in Header ***'
CheckBox ckBox = new CheckBox();
Rectangle rect = myDgv.GetCellDisplayRectangle(0, -1, true);
ckBox.Size = new Size(18, 18);
rect.X = rect.Location.X + (rect.Width / 4);
ckBox.Location = rect.Location;
ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);
this.myDgv.Controls.Add(ckBox);
myDgv.Columns(0).Frozen = false;
//*** Column (CustomerID) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "CustomerID";
ColumnTextBox.HeaderText = "CustomerID";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (Name) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "Name";
ColumnTextBox.HeaderText = "Name";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (Email) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "Email";
ColumnTextBox.HeaderText = "Email";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (CountryCode) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "CountryCode";
ColumnTextBox.HeaderText = "CountryCode";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (Budget) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "Budget";
ColumnTextBox.HeaderText = "Budget";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (Used) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "Used";
ColumnTextBox.HeaderText = "Used";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
}
Date :
2012-03-26 13:29:37
By :
webmaster
ตรงไหนที่เราต้องเปลี่ยนค่าบ้าง คับบบ
Date :
2012-03-26 14:23:12
By :
tomza
ถ้า sql = select Id,Name + space(2) + Lname form tbprofile
จะกำหนด
ColumnTextBox.DataPropertyName = "" <<<<<
ยังไงค่ะ
Date :
2013-02-24 18:38:09
By :
epukpui
Load balance : Server 03