C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Assemblies
.NET Core
.NET Standard
Active Directory
ADO.NET
Agile Development
AJAX
Alexa Skills
Algorithms in C#
Android
Angular
Architecture
ArcObject
Artificial Intelligence
ASP.NET
ASP.NET Core
Augmented Reality
Aurelia
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blockchain
Bootstrap
Bot Framework
Business
C#
C# Corner
C, C++, MFC
Career Advice
Chapters
CIO
Cloud
COBOL.NET
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Cortana Development
Cryptocurrency
Cryptography
Crystal Reports
Current Affairs
Custom Controls
Cyber Security
Data Mining
Databases & DBA
Design Patterns & Practices
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
Expression Studio
F#
Files, Directory, IO
Games Programming
GDI+
General
Generative Engine Optimization (GEO)
Google Cloud
Google Development
Graphics Design
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Internet & Web
Internet of Things
Ionic
iOS
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
kotlin
Leadership
Learn .NET
LightSwitch
LINQ
Machine Learning
Microsoft 365
Microsoft Office
Microsoft Phone
Mobile Development
Multithreading
Nano Banana
NetBeans
Networking
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Power BI
Printing in C#
Products
Progressive Web Apps
Project Management
Python
Q#
QlikView
R
React
Reports using C#
Robotics & Hardware
Ruby on Rails
Salesforce
Security
Servers
SharePoint
SignalR
Silverlight
Smart Devices
Software Testing
SQL Language
SQL Server
Startups
String in C#
Swift
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
WCF
Wearables
Web Development
Web Services
Web3
Windows 10
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML Standard
XML
XNA
XSharp
Register
Login
1
Answer
Filling datagridview cells after validating first row in C#
Jim Tat
8y
252
0
1
Active Directory
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();
}
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Post
Reset
Cancel
Answers (
1
)
Related Discussion