Autocomplete with Int values - c#

I have this method auto complete for names(string),
i want to create other method that completes Int Type, i try it but it says
cannot use LIKE for int type only for string
void AutoComplete()
{
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection Collection = new AutoCompleteStringCollection();
con.Open();
cmd = new SqlCommand("select * from stagiaire",con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
nom = dr.GetString(1).ToString();
Collection.Add(nom);
}
textBox1.AutoCompleteCustomSource = Collection;
con.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
DataView dv = new DataView(dt);
dv.RowFilter = string.Format("nom like '{0}%'", textBox1.Text);
dataGridView1.DataSource = dv;
}
private void Form1_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("select * from stagiaire",con);
da.Fill(dt);
}

for Column with DataType INTEGER use query like this:
select * from Table where Id like 1;
or convert it in to varchar:
select * from Table where CAST(Id AS VARCHAR(45)) like '1%'

Related

refreshing DataSource Master-Detail XtraGrid

i have this code that create Master-Detail XtraGrid at runtime
public partial class FRM_Reserved : DevExpress.XtraEditors.XtraForm
{
DataTable Table1 = new DataTable("Table1");
DataTable Table2 = new DataTable("Table2");
DataSet dataSet = new DataSet();
public FRM_Reserved()
{
InitializeComponent();
Table1 = ord.Get_Orders();
Table2 = ord.Get_Order_Res();
dataSet.Tables.Add(Table1);
dataSet.Tables.Add(Table2);
dataSet.Relations.Add("OrderDetails",
dataSet.Tables["Table1"].Columns["Bon N"],
dataSet.Tables["Table2"].Columns["Bon N"]);
gridControl3.DataSource = dataSet.Tables["Table1"];
}
I want to refreshing DataSource on a click of button so I added this code
private void btnRefresh_Click(object sender, EventArgs e)
{
Table1 = ord.Get_Orders();
Table2 = ord.Get_Order_Res();
dataSet.Tables.Add(Table1);
dataSet.Tables.Add(Table2);
gridControl3.DataSource = dataSet.Tables["Table1"];
gridControl3.ForceInitialize();
}
but the code does not work ,can you help me please.
Try below code. Hope it works
private void btnRefresh_Click(object sender, EventArgs e)
{
gridControl3.DataSource=null;
gridControl3.Items.Clear();
Table1 = ord.Get_Orders();
Table2 = ord.Get_Order_Res();
dataSet.Tables.Add(Table1);
dataSet.Tables.Add(Table2);
gridControl3.DataSource = dataSet.Tables["Table1"];
gridControl3.ForceInitialize();
}
the problem was in the name of the tables it change every time
dataSet.Tables[(Table1.TableName)]
dataSet.Tables[(Table2.TableName)]
so I made this changes
private void simpleButton1_Click(object sender, EventArgs e)
{
dataSet.Relations.Clear();
dataSet.Tables["Table2"].Clear();
dataSet.Tables["Table1"].Clear();
Table1 = ord.Get_Orders();
Table2 = ord.Get_Order_Res();
dataSet.Tables.Add(Table1);
dataSet.Tables.Add(Table2);
dataSet.Relations.Add("OrderDetails",
dataSet.Tables[(Table1.TableName)].Columns["Bon N"],
dataSet.Tables[(Table2.TableName)].Columns["Bon N"]);
gridControl3.DataSource = dataSet.Tables[(Table1.TableName)];
gridControl3.ForceInitialize();
}
Thanks for helping me
Use the following code
gridControl3.DataSource = dataSet.Tables["Table1"];
gridControl3.RefreshDataSource();
Try this one
private void button1_Click(object sender, EventArgs e)
{
var ds = new DataSet();
var dt1 = GetTable1Data();
var dt2 = GetTable2Data();
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
ds.Relations.Add("RelationTable",
ds.Tables["Table1"].Columns["col1"],
ds.Tables["Table2"].Columns["col1"]);
gridControl1.DataSource = ds.Tables["Table1"];
gridControl1.RefreshDataSource();
}
private DataTable GetTable1Data()
{
using (var conn = new SqlConnection("Conneciton string goes here"))
{
conn.Open();
using (var cmd = new SqlCommand("Stored procedure name goes here", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
var dt = new DataTable();
dt.Load(cmd.ExecuteReader());
return dt;
}
}
}
private DataTable GetTable2Data()
{
using (var conn = new SqlConnection("Conneciton string goes here"))
{
conn.Open();
const string sql = "SELECT * FROM Table2";
using (var cmd = new SqlCommand(sql, conn))
{
var dt = new DataTable();
dt.Load(cmd.ExecuteReader());
return dt;
}
}
}

C# System.Data.DataRowView

I am almost there trying to filter my datagridview from a combobox avoiding to using the databinding GUI. My problem is now that I see System.Data.DataRowView in my combobox inxtead of the normal values. The code:
private void Form2_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(#"Data SourceXXXXX;Initial Catalog=Studio;Integrated Security=True;");
conn.Open();
SqlCommand sc = new SqlCommand("SELECT Nome FROM DClub order by Nome", conn);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("Nome", typeof(string));
dt.Load(reader);
comboBox1.ValueMember = "Nome";
comboBox1.DisplayMember = "Nome";
comboBox1.DataSource = dt;
conn.Close();
var select = "SELECT *,bell+corp+fasc+app as Obi, bell+corp+fasc+vic+app as Tot,(bell+corp+fasc+vic+app)/5 as Med, (bell+corp+fasc+app)/4 as MedOb FROM DClub order by bell+corp+fasc+vic+app desc";
var c = new SqlConnection("Data Source=DIEGOPC;Initial Catalog=Studio;Integrated Security=True;"); // Your Connection String here
var dataAdapter = new SqlDataAdapter(select, c);
var commandBuilder = new SqlCommandBuilder(dataAdapter);
var ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = ds.Tables[0];
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var dataView = ((DataTable)dataGridView1.DataSource).DefaultView;
if (comboBox1.Text == "Remove filter")
{
dataView.RowFilter = string.Empty;
}
else
{
dataView.RowFilter = $"Nome = '{comboBox1.Text}'";
}
}
}
}
In your last comment
combo1text contains the values retrieved from the query
is fine however what I meant was what does the string itself look like. I assume it is a string that would match one of the values in the Nome column? Without seeing what this “query” strings values are in the combo box, it is difficult to give a good answer. Below, I added a DataGridView and a ComboBox to a form then populated the DataGridView and ComboBox with two DataTables using some sample data. This seems to work as you describe. I assume the row filter line: dataView.RowFilter = $"Nome = '{comboBox1.Text}'"; is simply filtering on matches in DataTables Nome column. I hope the code below helps.
DataTable dgvData;
DataTable cbData;
public Form1() {
InitializeComponent();
dgvData = GetDVGData();
cbData = GetCBData(dgvData);
dataGridView1.DataSource = dgvData;
this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);
comboBox1.DisplayMember = "Filter";
comboBox1.ValueMember = "Filter";
comboBox1.DataSource = cbData;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
}
private DataTable GetCBData(DataTable inDT) {
DataTable dt = new DataTable();
dt.Columns.Add("Filter", typeof(string));
dt.Rows.Add("Remove Filter");
foreach (DataRow dr in inDT.Rows) {
dt.Rows.Add(dr.ItemArray[1].ToString());
}
return dt;
}
private DataTable GetDVGData() {
DataTable dt = new DataTable();
dt.Columns.Add("Number");
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Rows.Add("1", "John", "Texas");
dt.Rows.Add("2", "Mary", "Florida");
dt.Rows.Add("3", "Tom", "New York");
dt.Rows.Add("4", "Marvin", "Moon");
dt.Rows.Add("5", "Jason", "Holland");
dt.Rows.Add("6", "Teddy", "New York");
return dt;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
var dataView = ((DataTable)dataGridView1.DataSource).DefaultView;
if (comboBox1.Text == "Remove Filter") {
dataView.RowFilter = string.Empty;
}
else {
dataView.RowFilter = $"Name = '{comboBox1.Text}'";
}
}

AutoComplete Textbox with DataGridView [duplicate]

First to my problem: Id like to filter my DataGrid with a TextBox dynamically for the Name.
My current code:
how the DataGrid is filled:
cnb1 = new MySqlConnectionStringBuilder();
cnb1.Server = "xxxxxxxxxxxxxxxxxxx";
cnb1.Database = "xxxxxxxxxxxxxxx";
cnb1.UserID = "xxxxxxxxxxxxxxxxx";
cnb1.Password = "xxxxxxxxxxxxxxxxxx";
cn1 = new MySqlConnection(cnb1.ConnectionString);
cn1.Open();
da = new MySqlDataAdapter("SELECT * FROM AktuelleDaten", cn1);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.SelectCommand.ExecuteNonQuery();
ds = new DataSet();
da.Fill(ds);
hauptübersichtgrid.ItemsSource = ds.Tables[0].DefaultView;
The Code for the TextBox:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
DataSet DV = ds;
hauptübersichtgrid.ItemsSource = DV.Tables[0].DefaultView.RowFilter = string.Format("Name like '%{0}%'", Filter.Text);
}
It looks like this:
Change your code to this:
private void TextBox_TextChanged(object sender, EventArgs e)
{
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = string.Format("Name like '%{0}%'", Filter.Text);
hauptübersichtgrid.ItemsSource = dv;
}

Filter DataGrid for name with TextBox dynamically

First to my problem: Id like to filter my DataGrid with a TextBox dynamically for the Name.
My current code:
how the DataGrid is filled:
cnb1 = new MySqlConnectionStringBuilder();
cnb1.Server = "xxxxxxxxxxxxxxxxxxx";
cnb1.Database = "xxxxxxxxxxxxxxx";
cnb1.UserID = "xxxxxxxxxxxxxxxxx";
cnb1.Password = "xxxxxxxxxxxxxxxxxx";
cn1 = new MySqlConnection(cnb1.ConnectionString);
cn1.Open();
da = new MySqlDataAdapter("SELECT * FROM AktuelleDaten", cn1);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.SelectCommand.ExecuteNonQuery();
ds = new DataSet();
da.Fill(ds);
hauptübersichtgrid.ItemsSource = ds.Tables[0].DefaultView;
The Code for the TextBox:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
DataSet DV = ds;
hauptübersichtgrid.ItemsSource = DV.Tables[0].DefaultView.RowFilter = string.Format("Name like '%{0}%'", Filter.Text);
}
It looks like this:
Change your code to this:
private void TextBox_TextChanged(object sender, EventArgs e)
{
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = string.Format("Name like '%{0}%'", Filter.Text);
hauptübersichtgrid.ItemsSource = dv;
}

update Cascading combobox problem C#

i have a simple question
i have a form which contain two related combo boxes but i have problem in updating the second dropdown content
this is the code
private void DiaryForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'expensesDataSet.Item' table. You can move, or remove it, as needed.
// this.itemTableAdapter.Fill(this.expensesDataSet.Item);
using (OleDbConnection con = dbconn.dbconnection())
{
ds1 = new ExpensesDataSet();
string sql = "SELECT * From DiaryView";
da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
da.Fill(ds1, "DiaryView");
NavigateRecords();
MaxRows = ds1.Tables["DiaryView"].Rows.Count;
//fill category combobox
string sqlcat = "SELECT * From Category";
catda = new System.Data.OleDb.OleDbDataAdapter(sqlcat, con);
catda.Fill(ds1, "Category");
catda.Update(ds1, "Category");
comboBox2.DataSource=ds1.Tables["Category"];
comboBox2.DisplayMember = "cat_name";
comboBox2.ValueMember="cat_id";
//comboBox1.Enabled = false;
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
using (OleDbConnection con = dbconn.dbconnection())
{
if (comboBox2.Items.Count > 0)
{
{
string cat = comboBox2.SelectedValue.ToString();
comboBox1.Enabled = true;
int catid = int.Parse(comboBox2.SelectedValue.ToString());
string sqlitem = "SELECT * From Item where cat_id = " + catid;
catda = new System.Data.OleDb.OleDbDataAdapter(sqlitem, con);
this.itemBindingSource.EndEdit();
catda.Fill(ds1, "Item");
catda.Update(ds1, "Item");
comboBox1.DataSource = ds1.Tables["Item"];
comboBox1.DisplayMember = "item_name";
comboBox1.ValueMember = "item_id";
}
}
}
}
there is two tables:
category(cat_id,cat_name)
item(item_id,item_name,cat_id)
what can i do??
plz help :)
If you want to clear the combobox, just do:
combobox1.Items.Clear();
It's as easy as that.

Categories