i have DataGridView for Invoive Save but i Have Problem
if i press Key "A" Item Show which Start "A"
But i Want when i Press "A" Item Show All the Words have "A", Such as SQL Query %Like% Statement
Can Someone Help Me this Problem
What I have tried:
public SaleWindow()
{
InitializeComponent();
dataGridView1.ColumnCount = 6;
dataGridView1.Columns[0].Name = "ProductName";
dataGridView1.Columns[1].Name = "QTY";
dataGridView1.Columns[2].Name = "Price";
dataGridView1.Columns[3].Name = "GST%";
dataGridView1.Columns[4].Name = "GST(Rs)";
dataGridView1.Columns[4].DefaultCellStyle.Format = "0.00";
dataGridView1.Columns[5].Name = "Total";
dataGridView1.Columns[5].DefaultCellStyle.Format = "0.00";
dataGridView1.Rows.Add();
}
public AutoCompleteStringCollection Cilentlistdropdown()
{
AutoCompleteStringCollection asc = new AutoCompleteStringCollection();
try
{
string Query;
Query = "Select Product_Name From Product";
SqlDataReader dr;
con.Open();
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;
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DataConnectionString"].ConnectionString);
SqlCommand cmd;
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
TextBox TB = e.Control as TextBox;
if (TB != null)
{
TB.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
TB.AutoCompleteCustomSource = Cilentlistdropdown();
TB.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
else
{
TextBox TB = e.Control as TextBox;
if (TB != null)
{
TB.AutoCompleteMode = AutoCompleteMode.None;
}
}
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPrass);
}
}
private void Control_KeyPrass(object sender,KeyPressEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
if (dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
{
string newvalue;
newvalue = (dataGridView1[e.ColumnIndex, e.RowIndex].Value).ToString();
if (newvalue != "")
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select Product_Name, Product_MRP, Product_GST_Rate from Product where Product_Name = '" + newvalue + "'", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
dataGridView1.Rows[e.RowIndex].Cells[0].Value = dt.Rows[0][0].ToString();
dataGridView1.Rows[e.RowIndex].Cells[2].Value = dt.Rows[0][1].ToString();
dataGridView1.Rows[e.RowIndex].Cells[3].Value = dt.Rows[0][2].ToString();
con.Close();
}
else
{
SqlDataAdapter da1 = new SqlDataAdapter("select top 1 Product_Name, Product_MRP, Product_GST_Rate from Product", con);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
if (dt1.Rows.Count > 0)
{
dataGridView1.Rows[e.RowIndex].Cells[0].Value = dt1.Rows[0][0].ToString();
dataGridView1.Rows[e.RowIndex].Cells[2].Value = dt1.Rows[0][1].ToString();
dataGridView1.Rows[e.RowIndex].Cells[3].Value = dt1.Rows[0][2].ToString();
con.Close();
}
}
}
}
else
{
SqlDataAdapter da1 = new SqlDataAdapter("select top 1 Product_Name, Product_MRP, Product_GST_Rate from Product", con);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
if (dt1.Rows.Count > 0)
{
dataGridView1.Rows[e.RowIndex].Cells[0].Value = dt1.Rows[0][0].ToString();
dataGridView1.Rows[e.RowIndex].Cells[2].Value = dt1.Rows[0][1].ToString();
dataGridView1.Rows[e.RowIndex].Cells[3].Value = dt1.Rows[0][2].ToString();
con.Close();
}
}
if (dataGridView1[0, e.RowIndex].Value != null)
{
int a = Convert.ToInt32(dataGridView1[1, e.RowIndex].Value);
if (dataGridView1[1, e.RowIndex].Value == null || a <= 0 || dataGridView1[1, e.RowIndex].Value.ToString() == "" || dataGridView1[1, e.RowIndex].Value.ToString() == "0" || string.IsNullOrWhiteSpace(dataGridView1[1, e.RowIndex].Value.ToString()))
{
dataGridView1["QTY", e.RowIndex].Value = "1";
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[1];
dataGridView1.BeginEdit(true);
}
}
}
if (e.ColumnIndex == 1)
{
if (dataGridView1[0, e.RowIndex].Value != null)
{
decimal a = Convert.ToDecimal(dataGridView1[1, e.RowIndex].Value);
if (dataGridView1[1, e.RowIndex].Value == null || a <= 0 || dataGridView1[1, e.RowIndex].Value.ToString() == "" || dataGridView1[1, e.RowIndex].Value.ToString() == "0" || string.IsNullOrWhiteSpace(dataGridView1[1, e.RowIndex].Value.ToString()))
{
dataGridView1["QTY", e.RowIndex].Value = "1";
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[1];
dataGridView1.BeginEdit(true);
}
}
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[dataGridView1.Columns["Total"].Index].Value = (Convert.ToDouble(row.Cells[dataGridView1.Columns["Price"].Index].Value) * Convert.ToDouble(row.Cells[dataGridView1.Columns["QTY"].Index].Value));
row.Cells[dataGridView1.Columns["GST(Rs)"].Index].Value = (Convert.ToDouble(Convert.ToDouble(row.Cells[dataGridView1.Columns["Total"].Index].Value) * (Convert.ToDouble(row.Cells[dataGridView1.Columns["GST%"].Index].Value)) / (100 + (Convert.ToDouble(row.Cells[dataGridView1.Columns["GST%"].Index].Value)))));
}
}
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView1[0, e.RowIndex].Value != null)
{
decimal a = Convert.ToDecimal(dataGridView1[1, e.RowIndex].Value);
if (dataGridView1[1, e.RowIndex].Value == null || a <= 0 || dataGridView1[1, e.RowIndex].Value.ToString() == "" || string.IsNullOrWhiteSpace(dataGridView1[1, e.RowIndex].Value.ToString()))
{
dataGridView1["QTY", e.RowIndex].Value = "1";
}
}
}