Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Bounty
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Aspire
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
Apple
Arbitrum
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Base Blockchain
Big Data
Bitcoin
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Docker
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Kubernetes
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Prompt Engineering
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
RevealBi
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Vibe Coding
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
1
Answer
Filling datagridview cells after validating first row in C#
Jim Tat
8y
2.2k
0
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.