Conditionally display columns in a datagrid

rennilmoksa

New Member
I have a datagrid which displays the results of a criteria typed into a search engine.<BR><BR>One of the columns is is the URL of the page matched to the criteria. Some of these will be pages are just flat text files which are usually loaded into a content area of a template file. Others are pages which can be directly linked to.<BR><BR>So I need to place an 'IF' statement somewhere to say 'if this URL is a flat text file then load another.aspx?file=matchedURL' otherwise just display the matched URL. And the another.aspx file will load the text into the content area.<BR><BR>The question is where do you place the conditional statement? It can't be done inline as such code is not accepted there. Is it done prior to binding? Or do you override one of the render methods of the page class?<BR><BR>I'm a little confused.....<BR><BR>Here's the datagrid code<BR><BR> <asp:DataGrid id="SearchGrid"<BR> AutoGenerateColumns="false"<BR> PageSize="10"<BR> AllowPaging="true"<BR> Width="585"<BR> CellSpacing="0"<BR> CellPadding="0"<BR> BorderColor="#cccccc"<BR> BorderWidth="0"<BR> Font-Name="Verdana"<BR> Font-Size="9pt"<BR> OnPageIndexChanged="PageIndex_Changed"<BR> runat="server"><BR> <PagerStyle Mode="NumericPages" Font-Bold="true"/><BR> <HeaderStyle Font-Bold="true" BackColor="#f3f3f3"<BR> ForeColor="#990000"/><BR> <ItemStyle VerticalAlign="top"/><BR> <FooterStyle BackColor="#cccccc"/><BR> <Columns><BR> <asp:BoundColumn<BR> DataField="RANK"<BR> DataFormatString="{0,-2:##0.#}%"<BR> HeaderText="Rank"><BR> <ItemStyle Width="70"/><BR> </asp:BoundColumn><BR> <asp:TemplateColumn<BR> HeaderText="Document information"><BR> <ItemStyle Width="515"/><BR> <ItemTemplate><BR> <p><a href=http://aspmessageboard.com/archive/index.php/"<%# DataBinder.Eval(Container, "DataItem.VPath")%>"><BR> <%# DataBinder.Eval(Container, "DataItem.DocTitle") %></a><BR> <br /><BR> <%# DataBinder.Eval(Container, "DataItem.Characterization")%>...<BR> <br /><BR> <a href="http://<%=Request.ServerVariables["SERVER_NAME"].Trim()%><BR> <%# DataBinder.Eval(Container, "DataItem.VPath")%>"<BR> style="word-wrap: break-word;"><BR> http://<%=Request.ServerVariables["SERVER_NAME"].Trim()%><%# DataBinder.Eval(Container, "DataItem.VPath")%><BR> </a><BR> <br /><BR> <i>Last modified: <%# DataBinder.Eval(Container, "DataItem.Write")%></i><BR> <br /> </p><BR> </ItemTemplate><BR> </asp:TemplateColumn><BR> </Columns><BR> </asp:DataGrid>It sounds like you could use the onItemDataBound event.....this only allows me to use a particular value in the datagrid eleswhere not actually conditionally change the value in the datagrid when it is displayed<BR><BR>anyone got any ideas?This is just an Idea and Also PSEUDOCODE, but may orientate you,<BR><BR>Try something like:<BR>------------------------------------<BR>Sub Analyse_procedure (Arguments..)<BR> Dim anString As String<BR> Dim SqlQuery As String<BR> Dim RowCounter As Integer<BR><BR> SqlQuery="select RowCount into RowCounter from yourTable where YourLinkField='<a href=*'>"<BR> Open Conexion<BR> Execute Command (SqlQueryInto, conexion)<BR> Close Conexion<BR> If RowCounter>0 then<BR> go to aspxPage1<BR> else<BR> go to aspxPage2<BR> End If<BR>End Sub <BR>------------------------------------<BR><BR>I Hope it is useful, <BR><BR>Salvador Gallego.thanks but thats not reall what I'm after...<BR><BR>what I want to do is check if the value in <%# DataBinder.Eval(Container, "DataItem.VPath")%> contains this string '/news/news_arti/'. If it does I want to build a href="1.aspx?q=DataBinder.Eval(Container, "DataItem.VPath", if not then just build <a href="<%# DataBinder.Eval(Container, "DataItem.VPath")%>"<BR><BR>now I realise thsat kind of processing doesn't work it was just to demonstrate what Im trying to do.<BR>Use a datatable and create rows with the same binding value. when you are building your rows only add the ones that meet your requirement:<BR><BR> Dim conn As New SqlConnection(Application.Item("conn"))<BR> Dim com As SqlCommand<BR> Dim sql As String<BR> Dim dt As DataTable<BR> Dim drow As DataRow<BR> Dim dc As DataColumn<BR> Dim dr As SqlDataReader<BR> sql = "SELECT string FROM string_table"<BR> com = New SqlCommand(sql, conn)<BR> conn.Open()<BR> dr = com.ExecuteReader<BR> dt = New DataTable("table")<BR> dc = New DataColumn("name", GetType(String))<BR> dt.Columns.Add("name")<BR> dim dval as string<BR> Try<BR> While dr.Read<BR> dval = dr.GetString(3)<BR> If instr(dval) > 0 Then<BR> drow = dt.NewRow()<BR> drow("name") = dval<BR> End If<BR> End While<BR> dg.DataSource = dt<BR> dg.DataBind()<BR> Catch<BR> Response.Write("There was an error: <BR>")<BR> Response.Write(Err.Description)<BR> Response.Write("<BR>SQL Statement:" & sql)<BR> End Try<BR><BR> conn.Close()<BR> conn.Dispose()<BR> com.Dispose()<BR><BR>In your aspx page:<BR><%# DataBinder.Eval(Container, "DataItem.name")%>
 
Back
Top