4

我有两gridview textbox列。我在gridview'textbox列中单独使用自动完成,但两gridview textbox列的自动完成都在这些列中混合数据。

如何在不混合来自article_code和的数据的情况下显示自动完成yarn_count

这是我的代码:

    AutoCompleteStringCollection namesCollection4=new AutoCompleteStringCollection(); 
string query = "select article_code from article_production";
con.Open();
SqlCommand cmb = new SqlCommand(query, con);
SqlDataReader dr = cmb.ExecuteReader();
if (dr.HasRows == true)
{
    while (dr.Read())
    {
        namesCollection4.Add(dr["article_code"].ToString());

    }
}
else
{
    MessageBox.Show("Data not found");
}

con.Close();
dr.Close();
int column = dataGridView1.CurrentCell.ColumnIndex;

if(column==8)
{
    TextBox tb = e.Control as TextBox;

    if (tb != null)
    {
        tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        tb.AutoCompleteCustomSource = namesCollection4;
        tb.AutoCompleteSource = AutoCompleteSource.CustomSource;

    }

}

第二个 Gridview 列

 AutoCompleteStringCollection namesCollection1=new AutoCompleteStringCollection(); 
string query1 = "select yarn_count from yarn";
con.Open();
SqlCommand cmb1 = new SqlCommand(query1, con);
SqlDataReader dr1 = cmb1.ExecuteReader();
if (dr1.HasRows == true)
{
    while (dr1.Read())
    {
        namesCollection1.Add(dr1["yarn_count"].ToString());

    }
}
else
{
    MessageBox.Show("Data not found");
}

con.Close();
dr1.Close();
int column1 = dataGridView1.CurrentCell.ColumnIndex;

if (column1 == 9)
{

    TextBox tb1 = e.Control as TextBox;

    if (tb1 != null)
    {
        tb1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        tb1.AutoCompleteCustomSource = namesCollection1;
        tb1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }

}
4

2 回答 2

1
 AutoCompleteStringCollection namesCollection1=new AutoCompleteStringCollection(); 
string query1 = "select yarn_count from yarn";
con.Open();
SqlCommand cmb1 = new SqlCommand(query1, con);
SqlDataReader dr1 = cmb1.ExecuteReader();
if (dr1.HasRows == true)
{
    while (dr1.Read())
    {
        namesCollection1.Add(dr1["yarn_count"].ToString());

    }
}
else
{
    MessageBox.Show("Data not found");
}

con.Close();
dr1.Close();
int column1 = dataGridView1.CurrentCell.ColumnIndex;

if (column1 == 9)
{

    TextBox tb1 = e.Control as TextBox;

    if (tb1 != null)
    {
        tb1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        tb1.AutoCompleteCustomSource = namesCollection1;
        tb1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }

}
else
{
namesCollection1.Clear();
}
finally{con.Close();}

我找到了我忘记写的解决方案

于 2013-06-18T07:19:02.800 回答
0

我已经测试过,这应该可以按您的意愿工作:

//EditingControlShowing event handler for your DataGridView
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
        DataGridViewTextBoxEditingControl cellEdit = e.Control as DataGridViewTextBoxEditingControl;
        cellEdit.AutoCompleteCustomSource = new AutoCompleteStringCollection();
        cellEdit.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        cellEdit.AutoCompleteSource = AutoCompleteSource.CustomSource;
        int colIndex = dataGridView.CurrentCell.ColumnIndex;
        if (colIndex == 8) cellEdit.AutoCompleteCustomSource.AddRange(new string[] { "Aaaa", "Bbbb", "Cccc" });
        else if (colIndex == 9) cellEdit.AutoCompleteCustomSource.AddRange(new string[] { "1234", "5678" });            
}

我不确定你的类型namesCollection是什么,如果你想分配给AutoCompleteSource属性,你必须确定它的类型是AutoCompleteStringCollection.

于 2013-06-17T18:30:31.410 回答