
Hello Dharanidhar,
Here is the explanation for your question: Here we have two cases: Suppose if you want to display Description of a book. (Description column of a table)
Case 1: Displaying a substring along with "Read More ..." link where read more link redirects you to another page, at which the whole description is rendered.
To get this, Directly after binding data to a gridview.. modify the text on the gridview displayed as substring with Read More link is appeared.
Eg:
DataTable table = new DataTable();
grid.DataSource = table; // grid is my asp:gridview id. Normal way of getting table & assigning to the gridview.
grid.DataBind();
int i = 0;
foreach (DataRow row in table.Rows)
{
string x = row["Description"].ToString();
if (x.Length > 10)
{
grid.Rows[i].Cells[0].Text = x.Substring(0, 10) + "<a href=\"\">Read More </a>";
// In href specify the link where you want to give the complete details.
}
i++;
if (i >= table.Rows.Count)
{
return;
}
}
Case 2:
Without redirecting to an another page, if you want get the whole description to be displayed beneath the row itself. Use the concept of Nested Grid View and Java Script.
You want to display without page refresh or redirecting to another page, so it is all with java script to do.
Eg:
JavaScript:
<script type="text/javascript">
function showfiles(id) {
if (document.getElementById(id).style.display == "none") {
document.getElementById(id).style.display = "block";
}
else {
document.getElementById(id).style.display = "none";
}
}
</script>
aspx :
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" OnRowDataBound="WriteReadMore" DataKeyNames="AutoId" >
<Columns>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%# Eval("ReadMore") %> // Read More is the column which contains the substring.
<a onclick="showfiles('<%#Eval ("AutoId") %>')" href="#">Read More:</a>
<div style="display: none" id="<%# Eval("AutoId") %>">
<asp:GridView ID="GridViewNested" AutoGenerateColumns="false" ShowHeader="false" runat="server" EnableViewState="false" BackColor="Aquamarine"
Width="100%">
<Columns>
<asp:BoundField DataField="Description" />
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here, i used the concept of Nested GridView.
The main issue comes over here is we should and must use <columns> to embed an another gridview inside the parent gridview.
If we use columns, <%# Eval() %> should be used. Eval takes the value directly from the table column. So, the complete Description comes. Hence, while insertion itself we have to maintain a seperate column inside the table which contains the part i.e substring of the description to be shown.
First, Get the Substring using <%# Eval("ReadMore") %> then with a link.
<a onclick="showfiles('<%#Eval ("AutoId") %>')" href="#">Read More:</a> (In the above case)
In the nested gridview, on click we get the same table but the whole description part (or you may include any other columns that you want) will be rendered.
Code to get Nested Grid View:
protected void WriteReadMore(object sender, GridViewRowEventArgs e)
{
DataTable tablefull = new DataTable();
if (e.Row.RowType != DataControlRowType.DataRow) return;
if (e.Row.RowType == DataControlRowType.DataRow)
{
int autoid = int.Parse(grid.DataKeys[e.Row.RowIndex].Value.ToString());
GridView nestedgrid = (GridView)e.Row.FindControl("GridViewNested");
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "Select * From InboxDescription where AutoId = @AutoId";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@AutoId", autoid);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(tablefull);
}
}
}
nestedgrid.DataSource = tablefull;
nestedgrid.DataBind();
}
}
Note: If you had already created a table in the database. Now you want to add a column Read More( with allow nulls so as not to re-create the table).
My InboxDescription table contains AutoId, FileName, Description, ReadMore as columns
declare
@readmore varchar(50),
@Description varchar(200),
@FileName varchar(max),
@AutoId int
select @readmore = SUBSTRING([Description], 0, 5) from InboxDescription
select @AutoId = AutoId from InboxDescription
select @FileName = [FileName] from InboxDescription
select @Description = [Description] from InboxDescription
Update InboxDescription set [FileName] = @FileName, [Description] = @Description, ReadMore = @Readmore where AutoId = @AutoId
I hope, this gave you a clear Explanation.
Regards,
Awesome Coding !! :)
Dharanidhar, if this helps please login to Mark As Answer. | Alert Moderator