How to transfer the some data or all data from datagridview1 to datagridview2 in the same form with C# ? How to edit the data in datagridview1 or datagridview2 in any column and row and add the new row and remove the row too ?
// Method 1: Transfer All Rows private void TransferAllRows() { // Clear destination DataGridView first dataGridView2.Rows.Clear(); // Copy all rows from dataGridView1 to dataGridView2 foreach (DataGridViewRow row in dataGridView1.Rows) { if (!row.IsNewRow) // Exclude the new row template { dataGridView2.Rows.Add(row.Cells.Cast() .Select(cell => cell.Value).ToArray()); } } } // Method 2: Transfer Selected Rows private void TransferSelectedRows() { foreach (DataGridViewRow row in dataGridView1.SelectedRows) { if (!row.IsNewRow) { dataGridView2.Rows.Add(row.Cells.Cast() .Select(cell => cell.Value).ToArray()); } } } // Method 3: Transfer Specific Rows by Condition private void TransferFilteredRows() { var rowsToTransfer = dataGridView1.Rows .Cast() .Where(row => SomeCondition(row)) // Define your condition .ToList(); foreach (var row in rowsToTransfer) { dataGridView2.Rows.Add(row.Cells.Cast() .Select(cell => cell.Value).ToArray()); } } Editing a Cell: Find the exact row and column you want to change Use dataGridView1.Rows[rowIndex].Cells[columnIndex].Value = newValue Replace rowIndex and columnIndex with actual numbers Replace newValue with the text or data you want to put in that cell Adding a New Row: Use dataGridView1.Rows.Add() method Put the values for each column in order Can add values directly or create a new row first, then add values Deleting a Row: Two main ways to delete: a) Delete by row number: dataGridView1.Rows.RemoveAt(rowIndex) b) Delete selected row: dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]) Always check if rows exist before trying to delete Tips: rowIndex starts at 0 (first row is 0, second is 1) Use dataGridView1.Rows.Count to know how many rows exist Hope this helps
Thank you so much for these types of videos, I'm a 1st year IT student, I'm following your channel with two accounts of mine. Even if I preferred really that I find a couple of videos about MVVM with more than tow ViewModels, and more than two Models, when we show data from JSON/CSV files in as ObservableCollections with some CRUD, for example we have a User abstract class, and tow or three employee classes inherited, and Customer Class inherited from the User as well, another class of Vehicles or Parcels, since the app is to manage Transporting Parcels from a country to another. Well it's my idea which I'm trying to make it happen for my school project this first year. But, i'm still struggling with the concept of DataConext and binding observableCollections to a window since I have multiples Views and ViewModel and Models. I feel like I'm LOST and I can't really see clearly into it, any advices please? thank you :).
Hey @pcastuces2748, So you are a first year IT student, congratulations. I'm going to help you all the way through your project. Instead of giving you a list of video's to watch, and me writing this program for you, which will be a dis-service, let's take baby steps, and I'll make sure you become a competent programmer. I have a video about abstract class: ua-cam.com/video/K3wGNapaGPk/v-deo.html 1) watch this video 2) have any questions about video, send questions to me 3) i will answer, we go back-n-forth until you understand abstract classes. 4) you are now ready to program up your project with an Abstract class and supporting class objects. 5) send me your code, I will review and make recommendations Send homework to: softwareNugget65@gmail.com If you were give a Homework assignment, and it's written down, please send that to me ASAP. If it is not written down, YOU write down all the requirements of this project. *We can do this!* Scott
hey @jlei6802, Yes, if you want to use the Newtonsoft.Json library in a C# project, you typically need to install it using NuGet. Newtonsoft.Json is a popular JSON serialization/deserialization library for .NET, and NuGet is the package manager for .NET projects.
Ooh, nice and quick! Thanks :)
Hey @robinHeyer708,
Here is the video I told you, I'd make.
ua-cam.com/video/aicXtNmSHbg/v-deo.html
hope it is helpful.
Scott
How to transfer the some data or all data from datagridview1 to datagridview2 in the same form with C# ?
How to edit the data in datagridview1 or datagridview2 in any column and row and add the new row and remove the row too ?
// Method 1: Transfer All Rows
private void TransferAllRows()
{
// Clear destination DataGridView first
dataGridView2.Rows.Clear();
// Copy all rows from dataGridView1 to dataGridView2
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow) // Exclude the new row template
{
dataGridView2.Rows.Add(row.Cells.Cast()
.Select(cell => cell.Value).ToArray());
}
}
}
// Method 2: Transfer Selected Rows
private void TransferSelectedRows()
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
if (!row.IsNewRow)
{
dataGridView2.Rows.Add(row.Cells.Cast()
.Select(cell => cell.Value).ToArray());
}
}
}
// Method 3: Transfer Specific Rows by Condition
private void TransferFilteredRows()
{
var rowsToTransfer = dataGridView1.Rows
.Cast()
.Where(row => SomeCondition(row)) // Define your condition
.ToList();
foreach (var row in rowsToTransfer)
{
dataGridView2.Rows.Add(row.Cells.Cast()
.Select(cell => cell.Value).ToArray());
}
}
Editing a Cell:
Find the exact row and column you want to change
Use dataGridView1.Rows[rowIndex].Cells[columnIndex].Value = newValue
Replace rowIndex and columnIndex with actual numbers
Replace newValue with the text or data you want to put in that cell
Adding a New Row:
Use dataGridView1.Rows.Add() method
Put the values for each column in order
Can add values directly or create a new row first, then add values
Deleting a Row:
Two main ways to delete:
a) Delete by row number: dataGridView1.Rows.RemoveAt(rowIndex)
b) Delete selected row: dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0])
Always check if rows exist before trying to delete
Tips:
rowIndex starts at 0 (first row is 0, second is 1)
Use dataGridView1.Rows.Count to know how many rows exist
Hope this helps
Thank you :)
I hope you found this useful!
Thank you so much for these types of videos, I'm a 1st year IT student, I'm following your channel with two accounts of mine.
Even if I preferred really that I find a couple of videos about MVVM with more than tow ViewModels, and more than two Models, when we show data from JSON/CSV files in as ObservableCollections with some CRUD, for example we have a User abstract class, and tow or three employee classes inherited, and Customer Class inherited from the User as well, another class of Vehicles or Parcels, since the app is to manage Transporting Parcels from a country to another. Well it's my idea which I'm trying to make it happen for my school project this first year.
But, i'm still struggling with the concept of DataConext and binding observableCollections to a window since I have multiples Views and ViewModel and Models. I feel like I'm LOST and I can't really see clearly into it, any advices please?
thank you :).
Hey @pcastuces2748, So you are a first year IT student, congratulations. I'm going to help you all the way through your project.
Instead of giving you a list of video's to watch, and me writing this program for you, which will be a dis-service, let's take baby steps, and I'll make sure you become a competent programmer.
I have a video about abstract class:
ua-cam.com/video/K3wGNapaGPk/v-deo.html
1) watch this video
2) have any questions about video, send questions to me
3) i will answer, we go back-n-forth until you understand abstract classes.
4) you are now ready to program up your project with an Abstract class and supporting class objects.
5) send me your code, I will review and make recommendations
Send homework to:
softwareNugget65@gmail.com
If you were give a Homework assignment, and it's written down, please send that to me ASAP.
If it is not written down, YOU write down all the requirements of this project.
*We can do this!*
Scott
Do we really need the nugent package or can we do without?
hey @jlei6802, Yes, if you want to use the Newtonsoft.Json library in a C# project, you typically need to install it using NuGet. Newtonsoft.Json is a popular JSON serialization/deserialization library for .NET, and NuGet is the package manager for .NET projects.