Hi RPA,
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.Data.IO
Code
C#
private void Submit(object sender, EventArgs e)
{
string filePath = @"C:\Questions.txt";
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Question", typeof(string)),
new DataColumn("Option1", typeof(string)),
new DataColumn("Option2", typeof(string)),
new DataColumn("Option3", typeof(string)),
new DataColumn("Option4", typeof(string))
});
string[] qa = File.ReadAllLines(filePath);
for (int i = 0; i < qa.Length; i++)
{
DataRow dr = dt.NewRow();
dr["Question"] = qa[i];
dr["Option1"] = qa[i + 1];
dr["Option2"] = qa[i + 2];
dr["Option3"] = qa[i + 3];
dr["Option4"] = qa[i + 4];
dt.Rows.Add(dr);
i = i + 4;
}
this.dgvQuestions.DataSource = dt;
}
VB.Net
Private Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
Dim filePath As String = "C:\Questions.txt"
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Question", GetType(String)),
New DataColumn("Option1", GetType(String)), New DataColumn("Option2", GetType(String)),
New DataColumn("Option3", GetType(String)), New DataColumn("Option4", GetType(String))})
Dim qa As String() = File.ReadAllLines(filePath)
For i As Integer = 0 To qa.Length - 1
Dim dr As DataRow = dt.NewRow()
dr("Question") = qa(i)
dr("Option1") = qa(i + 1)
dr("Option2") = qa(i + 2)
dr("Option3") = qa(i + 3)
dr("Option4") = qa(i + 4)
dt.Rows.Add(dr)
i = i + 4
Next
Me.dgvQuestions.DataSource = dt
End Sub
Screenshot