conditional processing inside a datagrid?

S

sean

Hi There,

I am trying to make the slow transition from asp to asp .net, I want to
display datagrid items based on form name where form name is the only value
that stays the same.

How can I evaluate the text in (DataBinder.Eval(Container.DataItem,
"formname")) and then display multiple rows based on the value?

Could someone help me out with the syntax please?

Sean


<tr>
<td colspan=2><asp:Label Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "formname") %>' /></td></tr>
<tr>


<tr>
<td><b><asp:Label Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "lbl5") %>' />&nbsp;</b></td>
<td><asp:Label Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "frm5value") %>' /></td>
</tr>
<tr>
<td><b><asp:Label Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "lbl6") %>' />&nbsp;</b></td>
<td><asp:Label Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "frm6value") %>' /></td>
</tr>
 
W

William Ryan eMVP

Bind the grid to a DataView instead of a DataTable.

TO great a DataView based on a datatable, do the following :(Assume your
grid was originally bound to myDataTable);

DataView dv = myDataTable.DefaultView;

myDataGrid.DataSource = dv;
myDataGrid.DataBind();

Now, before you do the binding, you can specify a RowFilter criteria. In
doing so, only the values that aren't filtered out will appear in your
grid - just make sure you set the filter before you do the binding (in
winforms this won't matter but in ASP.NET the grid is rendered at Page_Load
so changing the filter won't do it here) If you look at any of my
Efficiently Using ADO.NET xxx http://www.knowdotnet.com/williamryan.html
articles here I go into sorting and using RowFilter in depth. You can also
just Search Knowdotnet for RowFilter at our search screen, there are many
examples.

HTH,

Bill

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
S

sean

Hi William,

I am having a little trouble with the syntax, could you please help me out?

Sean - thank in advance


<script language="vb" runat="server">
Public Sub Page_Load(Sender As System.Object, E As System.EventArgs)

Dim strImageID as Integer = 111

Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))


Dim ds as DataSet=New DataSet()
Dim da as New SQLDataAdapter()
Dim myCommand As SqlCommand = New SqlCommand("GetListingDetail",
myConnection)
myCommand.CommandType = CommandType.StoredProcedure

Dim pListingID As New SqlParameter("@listingID", SqlDbType.int)
pListingID.Value = strImageID
myCommand.Parameters.Add(pListingID)
myConnection.Open
da.SelectCommand=myCommand
da.Fill(ds, "tblListings")

myDataGrid.Datasource=ds.Tables("tblListings").DefaultView
myDataGrid.DataBind()

'ds.RowFilter "formname = 'Fertigation'
End Sub
</script>
<html>
<head></head>
<body>

<asp:DataGrid id="myDataGrid" runat="server" />

</body>
</html>
 
S

sean

Hi William,

I think I have worked it out now, the only thing I am not sure about is how
I can display only the rows I want to? For example in the datagrid I can
return all of the columns regardless of wether they have a value or not,
what I am trying to do is display the columns I explicitly say.

col1,col2,col3,col4 etc

Sean - thanks in advance for your answer



Dim strImageID as Integer = 111

Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))


Dim ds as DataSet=New DataSet()
Dim da as New SQLDataAdapter()
Dim myCommand As SqlCommand = New SqlCommand("GetListingDetail",
myConnection)
myCommand.CommandType = CommandType.StoredProcedure

Dim pListingID As New SqlParameter("@listingID", SqlDbType.int)
pListingID.Value = strImageID
myCommand.Parameters.Add(pListingID)
myConnection.Open
da.SelectCommand=myCommand
da.Fill(ds, "tblListings")
Dim dv as DataView = ds.Tables("tblListings").DefaultView

dv.RowFilter = "formname='Fertigation '"

myDataGrid.Datasource=dv
myDataGrid.DataBind()


<asp:DataGrid id="myDataGrid" runat="server" />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top