I create a function with the aim to show the concatenated string to the DataGrid that I made such:
file1.exe, file2.docx, file3.mp3
how to display that way? I have tried it but messagebox displays an empty string.
This is the code :
public void View()
{
string namaFile = string.Empty;
foreach (DataGridViewRow row in DGVDekripsi.Rows)
{
string NameFile = (string)row.Cells[0].Value;
namaFile += NameFile + ",";
}
MessageBox.Show(namaFile);
}
The only way I could see your code not working is if you had no rows in your DataGridView
, so there was nothing to iterate over. Even if you had several rows and they all had empty names, you'd at least get a string with several commas in it.
Also, here's a slightly shorter way to get your list of names, using LINQ:
var allNames = string.Join(",",
DGVDekripsi.Rows.Cast<DataRow>().Select(x => x.Field<string>(0)));