ListView as usercontrol with Auto complete functionality using C#.NET
**ListView as usercontrol with Auto complete functionality using C#.NET
**
this is the output..
- Create a UserControl and name it as MyControlforUserName
- Place a ListView Control in UserControl
- Write the functions for dynamically binding
- Write the Code for Sorting the ListView
- Returns the selected records
Whole User Control Program is Follow
public partial class MyControlforUserName : UserControl
{
public string uname,mtch;
public int id;
public string rd
{
get;
set;
}
DataTable dt = new DataTable();
public event EventHandler SelectedIndexChanged;
public MyControlforUserName()
{
InitializeComponent();
this.Load += new EventHandler(MyControlforUserName_Load);
this.listView1.SelectedIndexChanged += new EventHandler(listView1_SelectedIndexChanged);
listView1.FullRowSelect = true;
}
private void MyControlforUserName_Load(object sender, EventArgs e)
{
bindlistview();
}
public void bindlistview()
{
SelectQuery sel = new SelectQuery();
dt.Clear();
listView1.Items.Clear();
dt = sel.SelectUserroledetails(); //DataTable is binded with user name. I place the Query in Class named SelectQuery
listView1.View = View.Details; //Listview to display as Grid
for (int i = 0; i < dt.Rows.Count; i++)// To bind ListView with DataTable dt Rows
{
DataRow dr = dt.Rows[i];
ListViewItem listitem = new ListViewItem(dr["UserID"].ToString());
listitem.SubItems.Add(dr["UserName"].ToString());
listitem.SubItems.Add(dr["Role"].ToString());
listView1.Items.Add(listitem);
}
}
string match, searchdata; // global declaration of variables
public void SearchListViewItem(string text) //'text' is the string to search for matching records
{
searchdata = text;
ListView list = new ListView(); // Creates new ListView
foreach (ListViewItem item in listView1.Items)
{
match = item.SubItems[1].Text;
if(match.StartsWith(text))
{
list.Items.Add((ListViewItem)item.Clone()); // if a match is found, that record is added to new ListView list
}
}
foreach (ListViewItem vw in listView1.Items)
listView1.Items.Clear(); // Clearing the old ListView
foreach (ListViewItem it in list.Items)
listView1.Items.Add((ListViewItem)it.Clone()); // Adding all records ( that is matching records ) in list to old ListView listview1
listviewselecteditems(); // Calling this function to return the selected records.
}
public void listviewselecteditems()
{
if (listView1.SelectedItems.Count > 0) // if a selection is made, the corresponding records are retured
{
ListViewItem item = listView1.SelectedItems[0];
uname = item.SubItems[1].Text;
string s = item.SubItems[0].Text;
id = Convert.ToInt32(s);
}
else if (match != "")
uname = searchdata;
if (SelectedIndexChanged != null) SelectedIndexChanged(this, null);
}
private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e) //for avoiding column resizing
{
e.Cancel = true;
e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
}
Now create a window form. And place a textbox.
Drag and Drop the MyControlforUserName from toolbox to our form.
We have to take four events of textbox, TextChanged, KeyPress, Click and Leave events.
int us=0; private void textBox2_TextChanged(object sender, EventArgs e) { else if (us == 3) { myControlforUserName1.SearchListViewItem(textBox2.Text); textBox2.Text = myControlforUserName1.uname; us = 0; } else { textBox2.Text = myControlforUserName1.uname; } } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (Regex.IsMatch(e.KeyChar.ToString(), @"^[a-zA-Z]+$") == true) { us = 3; // textBox2.Text = e.KeyChar.ToString(); } } private void textBox2_Leave(object sender, EventArgs e) { textBox2.Text = myControlforUserName1.uname; myControlforUserName1.Visible = false; } private void textBox2_Click(object sender, EventArgs e) { myControlforUserName1.Visible = true; myControlforUserName1.BringToFront(); myControlforUserName1.Left = textBox2.Left; myControlforUserName1.Top = textBox2.Bottom + 1; myControlforUserName1.Visible = true; }
Wish you a great day..Happy Codding :)