Hi mukesh1,
Please take reference the below code and correct your code.
HTML
<asp:GridView ID="grdchat" runat="server" AutoGenerateColumns="false" OnDataBound="grdchat_DataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="col-md-3" id="dvMessage" runat="server">
<%# Eval("message") %></div>
<div class="col-md-3" id="dvName" runat="server">
<%# Eval("name") %></div>
<div class="col-md-3" id="dvCity" runat="server">
<%# Eval("City") %></div>
<div class="col-md-3" id="dvState" runat="server">
<%# Eval("State") %></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Web.UI.HtmlControls;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("message", typeof(string)),
new DataColumn("Name", typeof(string)),
new DataColumn("City",typeof(string)),
new DataColumn("State",typeof(string))});
dt.Rows.Add("Hey", "John Hammond", "New Jercy", "United State");
dt.Rows.Add("Hey", "Mudassar Khan", "Mumbai", "India");
dt.Rows.Add("Hey", "Suzanne Mathews", "Nagpur", "India");
dt.Rows.Add("Hey", "Robert Schidner", "Banglore", "India");
grdchat.DataSource = dt;
grdchat.DataBind();
}
}
protected void grdchat_DataBound(object sender, EventArgs e)
{
int count = grdchat.Rows.Count;
foreach (GridViewRow row in grdchat.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string message = (row.FindControl("dvMessage") as HtmlGenericControl).InnerHtml.Trim();
string name = (row.FindControl("dvName") as HtmlGenericControl).InnerHtml.Trim();
string city = (row.FindControl("dvCity") as HtmlGenericControl).InnerHtml.Trim();
string state = (row.FindControl("dvState") as HtmlGenericControl).InnerHtml.Trim();
}
}
}