How to display data in gridview on click?

Total Post:341

Points:2389
C#  Gridview 
 1325  View(s)
Ratings:
Rate this:

I have a gridview that becomes quite long with some information that could be somewhat hidden.

Here's my asp:

<asp:GridView ID="gvLogBody" runat="server" CssClass="Grid" AllowPaging="true" AllowSorting="true" PageSize="10" AutoGenerateColumns="true">
</asp:GridView>

And here's my code behind:

DataSet ds = new DataSet();
ds.Tables.Add("LogBody");
ds.Tables["LogBody"].Columns.Add("timeStamp");
ds.Tables["LogBody"].Columns.Add("name");
ds.Tables["LogBody"].Columns.Add("message");
foreach (LogObject l in logLines)
{
    ds.Tables["LogBody"].Rows.Add(l.TimeStamp, l.Name, l.Message);
}
gvLogBody.DataSource = ds.Tables["LogBody"].DefaultView;
gvLogBody.DataBind();

This gives me a gridview that looks like this: ____________________________________________________________________________________________

|timeStamp|                      name                     |            message             |

+---------+-----------------------------------------------+--------------------------------+

|01-01-01 | someLongAndQuiteUnnecesaryNameThatIWishToHide | someMessageThatIsMoreImportant |

+---------+-----------------------------------------------+--------------------------------+

What I want is something like this:

_________________________________________________________

|timeStamp|    name     |            message             |

+---------+-------------+--------------------------------+

|01-01-01 | clickToShow | someMessageThatIsMoreImportant |

+---------+-------------+--------------------------------+

And once the user clicks the text, it expands / opens a popup or something.

How can this be done?

  1. Post:134

    Points:940
    Re: How to display data in gridview on click?

    This piece of code will help you out.

    <asp:GridView ID="gvLogBody" runat="server"CssClass="Grid" AllowPaging="true" AllowSorting="true"
    PageSize="10" AutoGenerateColumns="false">
                        <Columns>                        <asp:BoundField DataField="timeStamp" HeaderText="timeStamp"/>
                            <asp:TemplateField HeaderText="name">
                                <ItemTemplate>
           <input type="button"value="clickToShow" onclick="alert('<%#Eval("name") %>')" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="message" HeaderText="message"/>
                        </Columns>
                    </asp:GridView>

      Modified On Apr-06-2018 05:56:09 AM

Answer