.NET Framework Developer Center >
Using Forums Forums
>
Off-Topic Posts (Do Not Post Here)
>
Dynamically Add Column to gridview
Dynamically Add Column to gridview
- Hi all,
Below is my requirment. I have a gridview with template and bound columns . The last column of the gridview is combination of Label and Hyperlink. For Eg . Consider Admin , Ranjani are Label and Team A and Team B are hyperlink.
When i click on Team A it will show related users. My Stored Proc is returning users and Team Names with "," separator.
all i want is to dynamically add the teams as separate hyperlink.
Below code is creating only one link button
<asp:GridView ID="gvTeamList" runat="server" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false" BorderStyle="None" HorizontalAlign="Left" BorderWidth="0" Width="100%" PageSize="5" OnRowDataBound="gvTeamList_RowDataBound" OnPageIndexChanging="gvTeamList_PageIndexChanging" OnSorting="gvTeamList_Sorting"> <AlternatingRowStyle BackColor="#F0F0F0" /> <HeaderStyle BackColor="#CCCCCC" Font-Bold="True" ForeColor="black" Font-Underline="false" HorizontalAlign="Left" VerticalAlign="Top" /> <PagerStyle HorizontalAlign="Right" /> <Columns> <asp:TemplateField> <HeaderStyle Width="5%" Font-Bold="True" ForeColor="black" /> <HeaderTemplate> <input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="cbxSelectCase" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Case No" SortExpression="CaseNo"> <HeaderStyle Width="10%" Font-Bold="True" ForeColor="black" /> <ItemTemplate> <asp:LinkButton ID="lnkCaseNo" Font-Underline="true" ForeColor="#831111" runat="server" Text="<%#Bind('CaseNo') %>" CommandName="SingPass" CommandArgument="<%#Bind('CaseNo')%>"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="LFFileRefNumber" HeaderStyle-Width="20%" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="black" DataField="LFFileRefNumber" SortExpression="LFFileRefNumber" /> <asp:BoundField HeaderText="CaseName" HeaderStyle-Width="25%" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="black" DataField="CaseName" SortExpression="CaseName" /> <asp:TemplateField HeaderText="Teams/Persons with Access to Case" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="black" SortExpression="UserName"> <ItemTemplate> <asp:Label ID="lblUser" runat="server" Text="<%#Bind('UserName') %>"></asp:Label> <asp:Label ID="lblComma" runat="server" Text="," Visible="false"></asp:Label> <asp:LinkButton ID="lnkbtnTeam" runat="server" Text="<%#Bind('TeamName') %>" Visible="false"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
.cs
protected void Page_Load(object sender, EventArgs e) { try { if (!IsPostBack) { pnlCaseAccessSearch.Attributes.Add("style", "display:block"); beobj.LFID = "fti5"; CaseInfoDS = serviceobj.getCaseAccess(beobj); if (CaseInfoDS.Tables.Count > 0) { Session["CaseInfoDS"] = CaseInfoDS; BindGridviewColumns(gvTeamList, CaseInfoDS); } } } catch (Exception ex) { TeamAccessLogger.LogEntry(ErrorType.Error, TeamAccessConstants.CaseAccess, "Page_Load", ex.ToString(), ex.ToString()); } } private void BindGridviewColumns(GridView gvTeamList, DataSet CaseInfoDS) { try { gvTeamList.DataSource = CaseInfoDS; gvTeamList.DataBind(); } catch (Exception ex) { TeamAccessLogger.LogEntry(ErrorType.Error, TeamAccessConstants.CaseAccess, "BindGridViewColumns", ex.ToString(), ex.ToString()); } } protected void gvTeamList_RowDataBound(Object sender, GridViewRowEventArgs e) { try { foreach (GridViewRow row in gvTeamList.Rows) { if (gvTeamList.Rows.Count > 0) { LinkButton lnk = (LinkButton)row.FindControl("lnkbtnTeam"); string txt = lnk.Text; char[] separator = { ',' }; string[] TeamNames = txt.Split(separator); int i = TeamNames.Length; if (i > 0) { for (int h = 0; h < i; h++) { HyperLink hlink = new HyperLink(); hlink.Text = TeamNames[h]; e.Row.Cells[4].Controls.Add(hlink); } } } } } catch (Exception ex) { } }
Please let me know how to achieve this
thanks in advance
- Moved byeryangMSFTMonday, November 09, 2009 8:00 AM (From:.NET Base Class Library)
All Replies
- Yes you can do that but the built in hyperlink column won't work because it expects a single URL. You'd have to create a custom column to do that. But is that really what you want? If you only have 1 or 2 teams then that would be fine but if you have 10 or 20 then suddenly the column gets too big to manage. Why don't you consider a modified version of the column? You could display a generic hyperlink called Teams that, when clicked, pops up a modal (or modeless) dialog containing the list of teams hyperlinked. Then your columns don't get out of wack, the hyperlink column works without having to muck with it and you can just pass the team value from the dataset as the data for the column. You can do either client-side or server-side processing to generate the team names and URLs. Server is a little more efficient but if user's only click on a single list of teams then you'll be doing extra processing for no reason.
If it is common to have just a single team then you can opt to special case the scenario where this is a single team and just display the team and the URL rather than going through a modal dialog. Alternatively if it is common to have a couple of teams then you might consider adding a couple of Team columns (each displayed as a hyperlink) and a More-type button that can be used to display additional teams beyond those displayed in the grid. The goal here though is to not have your columns become arbitrarily large as that will mess up the UI.
Michael Taylor - 11/4/09
http://p3net.mvps.org - Thanks Michael . Your right if the teams increase it will be complete mess. but as of now i dont think it will be more than 3 teams
- Hi,
Thanks for your post, but it is tend to get quicker and better response/support in http://forums.asp.net where asp experts live in.
Thanks,
Eric
Please remember to mark helpful replies as answers and unmark them if they provide no help.


