Mohammed Khadir

Mohammed Khadir

  • NA
  • 65
  • 11.3k

DataGridView Column[1] with auto suggest text

Jun 12 2017 9:32 AM
Hi,
I am having a datagridview in my application, i have used below code to get the column[0] to work as auto-suggest text from database, and for serial number i have used rowpostpaint method of datagridview to the column[0]. now i am getting auto-suggest column at datagridview column[0] and serial number also at column[0]. But What actually i want is Auto-Suggest to be placed in Column[1] but i tried alot to get it done, but couldn't happened, please check the code and help where i should alter my code to get datagridview column[1] as Auto-Suggest.
Below is the code
 
 
public formName(){
InitializeComponent();
dataGridView1.ColumnCount = 6;
dataGridView1.Columns[1].Name = "Product ID";
dataGridView1.Columns[2].Name = "Product Name";
dataGridView1.Columns[3].Name = "Price";
dataGridView1.Columns[4].Name = "Quantity";
dataGridView1.Columns[5].Name = "total";
}
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
this.dataGridView1.Rows[e.RowIndex].Cells["SlNo"].Value = (e.RowIndex + 1).ToString();
}
public AutoCompleteStringCollection ClientListDropDown()
{
AutoCompleteStringCollection asc = new AutoCompleteStringCollection();
try
{
string Query;
Query = "Select ProductName from Products";
SqlDataReader dr;
con.Open();
SqlCommand cmd = new SqlCommand(Query, con);
dr = cmd.ExecuteReader();
if ((dr != null) && (dr.HasRows))
while (dr.Read())
asc.Add(dr.GetValue(0).ToString());
dr.Close();
cmd.Dispose();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return asc;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
TextBox ProductID = e.Control as TextBox;
if (ProductID != null)
{
ProductID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
ProductID.AutoCompleteCustomSource = ClientListDropDown();
ProductID.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
else
{
TextBox ProductID = e.Control as TextBox;
if (ProductID != null)
{
ProductID.AutoCompleteMode = AutoCompleteMode.None;
}
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 0)
{
string newvalue;
newvalue = (dataGridView1[e.ColumnIndex, e.RowIndex].Value).ToString();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select ProductID, SP from Products where ProductName = '" + newvalue + "'", con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.Rows[e.RowIndex].Cells[2].Value = dt.Rows[0][0].ToString();
dataGridView1.Rows[e.RowIndex].Cells[3].Value = dt.Rows[0][1].ToString();
con.Close();
}
//cell multiplication
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[dataGridView1.Columns[5].Index].Value = (Convert.ToDouble(row.Cells[dataGridView1.Columns[3].Index].Value) * Convert.ToDouble(row.Cells[dataGridView1.Columns[4].Index].Value));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// sum in textbox
try
{
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[5].Value);
}
txtTotalAmount.Text = sum.ToString();
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
 
 
 
 
 
 
 
please help

Answers (2)