通用选择窗体
标签:datagridview style blog http io color os ar for
做一个程序下来,为了伺候好客户老爷子,基本上是把手输的都用选择代替。因此写一个通用的选择窗体很有必要。
如下,做了个简易的窗体。
里面代码如下,没什么难的。基本的注释也有。
public partial class SelectForm : Form
{
public string ReturnStr = "", ColumnValue = "", sql = "";
int ColumnIndex;
DataTable dt = null;
///
///
///
/// sql语句
/// 返回值的列名
public SelectForm(string sql, string ColumnValue)
{
this.ColumnValue = ColumnValue;
this.sql = sql;
InitializeComponent();
}
///
/// 页面LOAD事件
///
///
///
private void SelectForm_Load(object sender, EventArgs e)
{
dataGrid.CellDoubleClick += dataGrid_CellDoubleClick;//单元格Cell双击事件
DataBind();
}
///
/// 数据绑定
///
private void DataBind()
{
dt = GetDataTable();//根据sql语句绑定数据库
ColumnIndex = dt.Columns.IndexOf(ColumnValue);
dataGrid.DataSource = dt;
}
///
/// 根据sql语句,返回datatable
///
///
private DataTable GetDataTable()
{
SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
sb.DataSource = "liurt-pc";
sb.InitialCatalog = "mydb";
sb.UserID = "sa";
sb.Password = "qaz123";
using (SqlConnection con = new SqlConnection(sb.ToString()))
{
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
private void dataGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
ReturnStr = dataGrid.Rows[e.RowIndex].Cells[ColumnIndex].Value.ToString();
this.Close();
}
}
private void txtValue_TextChanged(object sender, EventArgs e)
{
DataRow[] drs = dt.Select(ColumnValue + " like ‘%" + txtValue.Text + "%‘");
if (drs.Length == 0)
{
dataGrid.DataSource = null;
}
else
{
dataGrid.DataSource = drs.CopyToDataTable();
}
}
private void dataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex % 2 != 0)
{ e.CellStyle.BackColor = Color.GreenYellow; }
else
{ e.CellStyle.BackColor = Color.MintCream; }
}
}
基本思路就是这样。
通用选择窗体
标签:datagridview style blog http io color os ar for
原文地址:http://www.cnblogs.com/liuruitao/p/4054887.html
文章来自:
搜素材网的
编程语言模块,转载请注明文章出处。
文章标题:
通用选择窗体
文章链接:http://soscw.com/essay/33309.html
评论