Saving a DataGridView

I open two files in 2 separate DGVs. After opening and pressing the "Format" button, it matches all the lines in two separate DGVs and copies them to the new DGV.

the first DGV looks like this (column labels):

Name P/NXY Rotation PkgStyle 

and the second DGV looks like this:

 PkgStyle P/D Feeder Vision Speed Machine Width Time 

The two files will match PkgStyle and combine the rest of the lines together to get a third DGV, which looks like this:

 Name P/NXY Rotation PkgStyle PkgStyle P/D Feeder Vision Speed Machine Width Time 

Now, since you can expect that there may be possible lines that do not match properly and will not combine two sets of lines from two files. If so, it will only enter information from the first file, not from the second.

So some sample data might look like this:

 Name P/NXY Rotation PkgStyle PkgStyle P/D Feeder Vision Speed Machine Width Time J11 1234 12 7 180 9876 9876 THETHING 1 1 1 UNI 12MM 1MS R90 2222 19 9 0 1255 1255 ITEM 2 1 1 UNI2 5MM 1MS J18 9845 11 4 270 456 C127 1111 05 1 270 5555 5555 ITEM2 3 1 1 UNI 8MM 0.1MS 

SO

The third row (not including column headers) contains empty cells in it. When the user changes the cell data in these empty places, I would like to add it to a text document that already contains similar data. Thus, if the user added 456 someITEM 4 1 1 UNI2 9MM 10MS to the rows of the DataGridView that were empty, I want to add this to the end of the file that already contains the data. I only want to add a row if all the cells in the column are filled for each row. The output may simply be limited space.

Can anyone help me with this?

+6
source share
2 answers
 foreach (var line in theList) { string pkgStyleNA, PDNA, feederNA, visionNA, speedNA, machineNA, widthNA, timeNA; if (line.PkgStyle.Equals(string.Empty)) pkgStyleNA = "N/A "; else pkgStyleNA = line.PkgStyle; if (line.PD.Equals(string.Empty)) PDNA = "N/A "; else PDNA = line.PD; if (line.Feeder.Equals(string.Empty)) feederNA = "N/A "; else feederNA = line.Feeder; if (line.Vision.Equals(string.Empty)) visionNA = "N/A "; else visionNA = line.Vision; if (line.Speed.Equals(string.Empty)) speedNA = "N/A "; else speedNA = line.Speed; if (line.Machine.Equals(string.Empty)) machineNA = "N/A "; else machineNA = line.Machine; if (line.Width.Equals(string.Empty)) widthNA = "N/A "; else widthNA = line.Width; if (line.Time.Equals(string.Empty)) timeNA = "N/A "; else timeNA = line.Time.ToString(); addToDataBase.Add(pkgStyleNA + " " + PDNA + " " + feederNA + " " + visionNA + " " + speedNA+ " " + machineNA+ " " + widthNA + " " + timeNA); } using (StreamWriter outFile = new StreamWriter(openDataBaseFile.FileName, true)) { outFile.WriteLine(); var noDuplicatesList = addToDataBaseList.Distinct().ToList(); foreach (var item in noDuplicatesList) outfile.WriteLine(item); } 
+1
source

You can use this link to check the selected cell null / empty or not with some modification: C # DataRow Empty-check

I suppose 3 datagrid: dg1, dg2, dg3 respectively. Columns dg3 are columns dg1 and dg2.

 public static string GetNewData(DataRow row) { string res = string.Empty; if (row == null) throw new ArgumentNullException("row"); foreach (DataColumn column in dg2.Columns) if (row.IsNull(column.ColumnName) || string.IsNullOrEmpty(row[column.ColumnName].ToString())) return string.Empty; else res += row[column.ColumnName] + " "; return res.Trim(); } 

I hope for this help.

+2
source

Source: https://habr.com/ru/post/896431/


All Articles