السلام عليكم
حبيت انقل الكم هذا الكود البسيط لكي تعم الفائده
كود يساعد على اظهار البياتات الموجوده في حقل ما في جدول بداخل قاعده البيانات في ال TextBox بشكل تلقائي وهذا يساعد في عمليات البحث !!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace AutoCompleteTextBox
{
public partial class Form1 : Form
{
AutoCompleteStringCollection autotext = new AutoCompleteStringCollection();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlDataReader reader;
SqlConnection cn = new SqlConnection("server=.; database=Students; integrated security= true");
cn.Open();
SqlCommand cm = new SqlCommand("select firstname from Infostudents ", cn);
reader = cm.ExecuteReader();
if (reader.HasRows )
{
while (reader.Read())
{
autotext.Add(reader["firstname"].ToString());
}
}
else
{
MessageBox.Show("Data not found");
}
reader.Close();
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = autotext;
}
}
}