添加控件:
bindingSource1和bindingNavigator1
支持datagridview下拉输入:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
string titleText=dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText;
if(titleText.Equals("name"))
{
TextBox autoText = e.Control as TextBox;
System.Diagnostics.Debug.Write(titleText + "\r");
if(autoText!=null)
{
System.Diagnostics.Debug.Write(autoText + "\r");
autoText.AutoCompleteMode = AutoCompleteMode.Suggest;
autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
DataCollection.Add("开发部");
DataCollection.Add("财务部");
autoText.AutoCompleteCustomSource = DataCollection;
}
}
}
升序排列:
private void btnAscend_Click(object sender, EventArgs e)
{
dataGridView1.Sort(dataGridView1.Columns[2], ListSortDirection.Ascending);
}
加载事件:
dataGridView1.RowsDefaultCellStyle.BackColor = Color.IndianRed;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Blue;
//总页数计算
totalPageNumber = (totalValNumber % valPage == 0) ? (totalValNumber / valPage) : (totalValNumber / valPage + 1);
toolStripTextBox1.Text = "1/"+totalPageNumber;
查询语句:
private void query_CX(DataGridView dt)//查询语句
{
OleDbConnection myCon = Getconnection();
try
{
myCon.Open();
string sql = "select * from 表1";
OleDbDataAdapter myda = new OleDbDataAdapter(sql, myCon);
DataSet myds = new DataSet();
myda.Fill(myds, "表1");
dt.DataSource = myds.Tables[0];
totalValNumber = myds.Tables[0].Rows.Count;
currentPage = StartVal / valPage + 1;
toolStripTextBox1.Text = currentPage.ToString() + "/" + totalPageNumber;
myds.Clear();
myda.Fill(myds,StartVal,valPage,"表1");
bindingSource1.DataSource = myds.Tables[0];
bindingNavigator1.BindingSource = bindingSource1;
dt.DataSource = bindingSource1;
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
finally
{
myCon.Close();
}
}