To answer your immediate question, BindDataCSV returns nothing (it has "void" as its return type) so you can't assign its return value to a variable as there is nothing to return.
To get back to my comments on your other thread, this code is not testable as it is. You need to extract the bits that are testable to their own class\functions. So I'd do something like change the BindDataCSV to;
private void BindDataCSV(string filePath)
{
string[] lines = System.IO.File.ReadAllLines(filePath);
DataTable dt = GetDataTable(lines);
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
}
Your unit tests shouldn't access any external resources like the file system, and ideally they shouldn't access GUI elements either as unit tests need to run automatically in any context without any dependencies. So the updated function extracts the important logic into a new function (GetDataTable) and does all the things you can't unit test like access files, update the data grid etc.
The GetDataTable function looks like this;
public DataTable GetDataTable(string[] lines)
{
DataTable dt = new DataTable();
if (lines.Length > 0)
{
string firstline = lines[0];
string[] headerLabels = firstline.Split(',');
foreach (string headerWord in headerLabels)
{
dt.Columns.Add(new DataColumn(headerWord));
}
for (int r = 1; r < lines.Length; r++)
{
string[] dataWords = lines[r].Split(',');
DataRow dr = dt.NewRow();
int columnIndex = 0;
foreach (string headerWord in headerLabels)
{
dr[headerWord] = dataWords[columnIndex++];
}
dt.Rows.Add(dr);
}
}
return dt;
}
This function acts on an array of strings and doesn't access the GUI or anything else, so this is now testable. I've written a few unit tests below, but it's up to you to decide everything you want to test.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void GetDataTable_ColumnsAreLoaded()
{
Form1 form1 = new Form1();
string[] lines = new string[] { "Col A, Col B", "Row 1 A, Row 1 B" };
DataTable dt = form1.GetDataTable(lines);
Assert.AreEqual(2, dt.Columns.Count);
}
[TestMethod]
public void GetDataTable_ColumnsAreCorrect()
{
Form1 form1 = new Form1();
string[] lines = new string[] { "Col A, Col B", "Row 1 A, Row 1 B" };
DataTable dt = form1.GetDataTable(lines);
Assert.AreEqual("Col A", dt.Columns[0].Caption);
Assert.AreEqual("Col B", dt.Columns[1].Caption);
}
[TestMethod]
public void GetDataTable_RowsAreLoaded()
{
Form1 form1 = new Form1();
string[] lines = new string[] { "Col A, Col B", "Row 1 A, Row 1 B" };
DataTable dt = form1.GetDataTable(lines);
Assert.AreEqual(1, dt.Rows.Count);
}
}
If you run these tests you'll see that GetDataTable_ColumnsAreCorrect actually fails, and it will tell you why. It was expecting "Col B" but got " Col B". Note the extra space before the column, and this is because you are not trimming the column names in GetDataTable, so that's a bug your unit tests have found already.
So that's what unit testing is about, it isn't a way of automating your whole process, it is about extracting the important logic and testing just that logic; I'm not touching BindDataCSV at all in my tests, instead I'm testing the logic. Also as you can see from above not all your code is going to be unit testable unless you have written it with unit tests in mind.