Hi aginell4life,
You need to create DataTable and insert the records in DataTable.
Then, the DataTable will be used to populate the GridView.
Refer the below example.
HTML
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Description1" HeaderText="Description1" />
<asp:BoundField DataField="Description2" HeaderText="Description2" />
<asp:BoundField DataField="Description3" HeaderText="Description3" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string searchedText = "red";
string message = "I have a red cap in my bag. The color of my car is red blended with blue Could you lend me your red shirt.";
string[] words = message.Split(' ');
DataTable dt = new DataTable();
dt.Columns.Add("Description1");
dt.Columns.Add("Description2");
dt.Columns.Add("Description3");
for (int i = 0; i < words.Length; i++)
{
if (words[i].ToLower() == searchedText.ToLower())
{
string previous = string.Empty;
string next = string.Empty;
string previous1 = string.Empty;
string next1 = string.Empty;
if (i > 0)
{
previous = words[i - 1];
previous1 = words[i - 2];
}
if (i < words.Length - 1)
{
next = words[i + 1];
}
if (i < words.Length - 2)
{
next1 = words[i + 2];
}
dt.Rows.Add(string.Format("{0} {1}", previous1, previous), searchedText, string.Format("{0} {1}", next, next1));
}
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim searchedText As String = "red"
Dim message As String = "I have a red cap in my bag. The color of my car is red blended with blue Could you lend me your red shirt."
Dim words As String() = message.Split(" "c)
Dim dt As DataTable = New DataTable
dt.Columns.Add("Description1")
dt.Columns.Add("Description2")
dt.Columns.Add("Description3")
For i As Integer = 0 To words.Length - 1
If words(i).ToLower() = searchedText.ToLower() Then
Dim previous As String = String.Empty
Dim [next] As String = String.Empty
Dim previous1 As String = String.Empty
Dim next1 As String = String.Empty
If i > 0 Then
previous = words(i - 1)
previous1 = words(i - 2)
End If
If i < words.Length - 1 Then
[next] = words(i + 1)
End If
If i < words.Length - 2 Then
next1 = words(i + 2)
End If
dt.Rows.Add(String.Format("{0} {1}", previous1, previous), searchedText, String.Format("{0} {1}", [next], next1))
End If
Next
gvDetails.DataSource = dt
gvDetails.DataBind()
End If
End Sub
Screenshot