SherrieThoroughman
New Member
I have a datagrid in a web form that gets its data from SQL Server database. It returns an image URL for each record and displays this image in the datagrid. Is there any way to stop it showing the actual URL in a separate column?You should specify your own columns instead of letting it autogenerate the columns. Then you can just specify the one column that you want to show up.How do you do this?<BR><BR>Many thanksThere are alot of options so you should check out some of the good datagrid articles on this site, but here is a copy of a simple datagrid with autogenerate turned off.<BR><BR><asp:datagrid id="dgCompanies" runat="server" Width="347px" CssClass="list2" AutoGenerateColumns="False" DataKeyField="id" ><BR><Columns><BR><asp:BoundColumn DataField="name" HeaderText="Company"></asp:BoundColumn><BR></Columns><BR></asp:datagrid>I had a similar need. After manually generating my columns, I added something like this to a procedure in my code-behind page:<BR><BR>DataGrid.Columns(2).Visible = False<BR><BR>,which makes my third column disappear.Do you know where this code needs to go, because when I try to compile it, it says:<BR><BR>c:inetpubwwwrootIndicatorIconDisplay.aspx.cs(128): 'System.Web.UI.WebControls.DataGrid.Columns' denotes a 'property' where a 'method' was expected<BR><BR>MAny thanks for any help,<BR>JamieThere might be a better way, but this should work:<BR><BR>*.aspx:<BR>-------<BR><BR><form id="Form1" method="post" runat="server"><BR> <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"><BR> <Columns><BR> <asp:BoundColumn DataField="ABC" HeaderText="ABC"></asp:BoundColumn><BR> <asp:BoundColumn DataField="XYZ" HeaderText="XYZ"></asp:BoundColumn><BR> </Columns><BR> </asp:datagrid><BR> <asp:radiobutton id="optHidden" runat="server" Text="Hidden" GroupName="Col0Visible" AutoPostBack="True"></asp:radiobutton><BR> <asp:radiobutton id="optVisible" runat="server" Text="Visible" GroupName="Col0Visible" AutoPostBack="True"></asp:radiobutton><BR></form><BR><BR>*.aspx.vb<BR>---------<BR><BR>Public Class WebForm1<BR> <BR> Inherits System.Web.UI.Page<BR> <BR> Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid<BR> Protected WithEvents optVisible As System.Web.UI.WebControls.RadioButton<BR> Protected WithEvents optHidden As System.Web.UI.WebControls.RadioButton<BR> <BR> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<BR> If Not Page.IsPostBack Then<BR> 'Set default value of optVisible/Hidden:<BR> optVisible.Checked = True<BR> Else<BR> 'Display or hide column "ABC":<BR> DataGrid1.Columns(0).Visible = (optVisible.Checked = True)<BR> End If<BR> 'Get data and bind grid here<BR> End Sub<BR><BR>End ClassOther Source:<BR>http://www.aspalliance.com/aldotnet/examples/hidecolumnarticle.aspx