Hi RivickJoe,
Refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
</asp:GridView><br />
<asp:Button Text="Refresh" runat="server" OnClick="Refresh" />
Namespaces
C#
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData("SELECT TOP 5 CustomerId,ContactName Name,Country FROM Customers");
}
}
private void GetData(string query)
{
string csvData = File.ReadAllText(Server.MapPath("~/Files/DataField.csv"));
int rowIndex = 0;
foreach (string row in csvData.Split('\n'))
{
if (rowIndex > 0)
{
if (!string.IsNullOrEmpty(row))
{
string[] columns = row.Split(',');
BoundField bfield = new BoundField();
bfield.DataField = columns[0];
bfield.HeaderText = columns[1];
bfield.ItemStyle.CssClass = columns[2];
bfield.ItemStyle.Width = Convert.ToInt16(columns[3]);
GridView1.Columns.Add(bfield);
}
}
rowIndex++;
}
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query, con);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
protected void Refresh(object sender, EventArgs e)
{
GridView1.Columns.Clear();
this.GetData("SELECT TOP 5 EmployeeId AS 'CustomerId',Firstname + ' ' + LastName AS 'Name',Country FROM Employees");
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.GetData("SELECT TOP 5 CustomerId,ContactName Name,Country FROM Customers")
End If
End Sub
Private Sub GetData(ByVal query As String)
Dim csvData As String = File.ReadAllText(Server.MapPath("~/Files/DataField.csv"))
Dim rowIndex As Integer = 0
For Each row As String In csvData.Split(vbLf)
If rowIndex > 0 Then
If Not String.IsNullOrEmpty(row) Then
Dim columns As String() = row.Split(","c)
Dim bfield As BoundField = New BoundField()
bfield.DataField = columns(0)
bfield.HeaderText = columns(1)
bfield.ItemStyle.CssClass = columns(2)
bfield.ItemStyle.Width = Convert.ToInt16(columns(3))
GridView1.Columns.Add(bfield)
End If
End If
rowIndex += 1
Next
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub Refresh(ByVal sender As Object, ByVal e As EventArgs)
GridView1.Columns.Clear()
Me.GetData("SELECT TOP 5 EmployeeId AS 'CustomerId',Firstname + ' ' + LastName AS 'Name',Country FROM Employees")
End Sub
Screenshots
The CSV file
The Form