Small is New Big in C#

Coding

Selecting Suggestion of Combobox

leave a comment »



private SqlConnection conn;

public SqlConnection GetSqlConn()
{
try
{
string strconn = "Server=local;Database=AdventureWorks;User id=sa;PWD=";
conn = new SqlConnection(strconn);
conn.Open();
return conn;
}
catch (Exception ex)
{
return null;
}
}


public void cboxBind(string sqlComm, string tableName, string tableColumn, ComboBox cbox)
{
DataSet ds = this.GetDataSet(sqlComm, tableName);
cbox.DataSource = ds.Tables[tableName];
cbox.DisplayMember = tableColumn;
}

public DataSet GetDataSet(string sqlComm, string tableName)
{
SqlConnection sqlconn = this.GetSqlConn();
SqlDataAdapter sqlda = new SqlDataAdapter(sqlComm, sqlconn);
DataSet ds = new DataSet();
sqlda.Fill(ds, tableName);
return ds;
}


public Form1()
{
InitializeComponent();

DataSet ds = this.GetDataSet("SELECT Distinct Name FROM Person.CountryRegion","Person.CountryRegion");
string[] list = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
list[i] = ds.Tables[0].Rows[i][0].ToString();
}

comboBox1.AutoCompleteCustomSource.AddRange(list);
comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
}

private void Form1_Load(object sender, EventArgs e)
{
this.cboxBind("SELECT DISTINCT Name FROM Person.CountryRegion", "Person.CountryRegion","Name", comboBox1);

}
Advertisements

Written by nauhil

June 18, 2010 at 2:03 pm

Posted in Tips of C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: