Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
1
Answer
Filling datagridview cells after validating first row in C#
Jim Tat
7y
243
1
Reply
I have a DataGridView of 4-5 columns.
The DataGridView is Editable.
When I enter a value in the Reference Column then immediately it will fill the other values in the others cells from mysql database.
But when I go to the next row and I want to add another value in the reference cell and then it will fill the other like the first one, it didn't do it...
This is what I tried....
private
void
TAB_Credit_CellEndEdit(
object
sender, DataGridViewCellEventArgs e)
{
if
(TAB_Credit.Columns[e.ColumnIndex].Name ==
"Reference"
)
{
string
cmdText = @"SELECT * FROM tb_ajout_articles
WHERE Reference=@
ref
";
MySqlDataAdapter sa =
new
MySqlDataAdapter(cmdText, MyConnexion);
sa.SelectCommand.Parameters.Add(
"@ref"
, MySqlDbType.VarChar).Value =
TAB_Credit.Rows[e.RowIndex].Cells[
"Reference"
].Value.ToString();
DataTable dt2 =
new
DataTable();
sa.Fill(dt);
if
(dt.Rows.Count == 1)
{
TXT_Stock.Text = dt.Rows[0][
"Quantite"
].ToString();
//double value = (double)TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value * (double)TAB_Credit.Rows[e.RowIndex].Cells["Prix_Unitaire"].Value;
TAB_Credit.Rows[e.RowIndex].Cells[
"Designation"
].Value = dt.Rows[0][
"Designation"
].ToString();
TAB_Credit.Rows[e.RowIndex].Cells[
"Quantite"
].Value = dt.Rows[0][
"Quantite"
].ToString();
TAB_Credit.Rows[e.RowIndex].Cells[
"Prix_Unitaire"
].Value = dt.Rows[0][
"Prix_Unitaire"
].ToString();
TAB_Credit[
"Total"
, e.RowIndex].Value =
Convert.ToString(
Convert.ToInt32(TAB_Credit[
"Quantite"
, e.RowIndex].Value) *
Convert.ToInt32(TAB_Credit[
"Prix_Unitaire"
, e.RowIndex].Value));
//TAB_Credit.Rows[e.RowIndex].Cells["Total"].Value = value.ToString();
TXT_Brut.Text = TAB_Credit[
"Total"
, e.RowIndex].Value.ToString();
}
}
if
(TAB_Credit.CurrentCell.ColumnIndex == 6)
{
if
(Convert.ToInt32(TAB_Credit.Rows[e.RowIndex].Cells[
"Quantite"
].Value) > Convert.ToInt32(TXT_Stock.Text))
{
MessageBox.Show(
"Stock finished"
);
TAB_Credit.Rows[e.RowIndex].Cells[
"Quantite"
].Value = dt.Rows[0][
"Quantite"
].ToString();
}
else
{
TAB_Credit[
"Total"
, e.RowIndex].Value =
Convert.ToString(
Convert.ToInt32(TAB_Credit[
"Quantite"
, e.RowIndex].Value) *
Convert.ToInt32(TAB_Credit[
"Prix_Unitaire"
, e.RowIndex].Value));
TXT_Brut.Text = TAB_Credit[
"Total"
, e.RowIndex].Value.ToString();
}
}
}
y app is a sort of cash register for a shop so in the datagrid there is Reference ,
it is the reference of the product.
When the user types the reference directly the information about the product will be filled in the other columns and the user can choose the quantity he wants and it will do the calculation.
ALSO, I tried to put the total of the bill in the datagrid...
Reference | Product |Quantity |Price |Total |Total Bill
A |AB |2 |1000 |2000 |5000
BC |B | |1000 |3000
This is what I am trying to do... To do it, I tried this code:
private
void
TAB_Credit_RowValidated(
object
sender, DataGridViewCellEventArgs e)
{
int
sum = 0;
for
(
int
i = 0; i < TAB_Credit.Rows.Count; ++i)
{
sum += Convert.ToInt32(TAB_Credit.Rows[i].Cells[
"Total"
].Value);
}
TAB_Credit.Rows[e.RowIndex].Cells[
"Total_Fact"
].Value = sum.ToString();
}
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
How to fill DataGridView Cells on cellvaluechanged in MYSQL
To fetch database value in CheckedListBox as Checked.