Let’s suppose you have CSV data and you want to show it in ASP.NET web application, for example, in GridView Control. This article will explain how to achieve this goal using C#.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SplitFunctionUsingRegx.aspx.cs"
Inherits="SplitFunctionUsingRegx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Original String<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br />
Separator<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="btnConvert" runat="server" Text="Split" OnClick="btnConvert_Click" />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
public partial class SplitFunctionUsingRegx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnConvert_Click(object sender, EventArgs e)
{
string str = TextBox1.Text;
string strSplit = TextBox2.Text;
ArrayList arSplit = new ArrayList();
Regex r = new Regex(strSplit);
string[] s = r.Split(str);
foreach (object o in s)
{
arSplit.Add(o);
}
GridView1.DataSource = arSplit;
GridView1.DataBind();
}
}