|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Read underlying data in GridView row?getting the data and the GridView is binding and displaying great. I’m stuck on something that would seem simple, but it is giving me fits. My query looks like this: Select [first],[Last],[Gender] FROM [tblNames] My Grid currently looks like this: First Last Gender Joe Smith M Jane Smith F I want my Grid to look like this: First Last Gender Joe Smith [Male Icon] Jane Smith [Female Icon] I want to add an image to each record based on the [Gender] field from the query, but, I don’t want the [Gender] data to show in the grid. I’ve tried a few things, but I can’t seem to figure out how to read the data from the underlying data record while iterating through the rows without showing the [Gender] data as a collumn. So far I’ve been using the “GridView1_dataBound†event to work with the e.Row(X).Cells(X).Controls(X), but I can’t figure out how to get at the data that is being applied to the dataView row. I think this should be a common task Can anyone point me in the right direction? Thanks! Bernie You can do that declaratively. If the values in Gender are either "M" or "F"
and you have 2 gifs that are named "M.gif" and "F.gif" then you can write something like this: <asp:ImageField DataImageUrlField="Gender" DataImageUrlFormatString ="{0}.gif" ></asp:ImageField> If the field values do not match the gif names you can do something like this: <asp:TemplateField > <ItemTemplate> <asp:Image ImageUrl='<%# Eval("Gender").ToString().Equals("M") ? "~/Images/Male.gif":"~/Images/Female.gif" %>' runat="server" /> </ItemTemplate> </asp:TemplateField> Show quoteHide quote "bernadou" wrote: > I have a simple ASP.NET 2.0 GridView that is bound to a simple query. I’m > getting the data and the GridView is binding and displaying great. I’m stuck > on something that would seem simple, but it is giving me fits. > > My query looks like this: > Select [first],[Last],[Gender] FROM [tblNames] > > My Grid currently looks like this: > > First Last Gender > Joe Smith M > Jane Smith F > > I want my Grid to look like this: > > First Last Gender > Joe Smith [Male Icon] > Jane Smith [Female Icon] > > I want to add an image to each record based on the [Gender] field from the > query, but, I don’t want the [Gender] data to show in the grid. I’ve tried a > few things, but I can’t seem to figure out how to read the data from the > underlying data record while iterating through the rows without showing the > [Gender] data as a collumn. > > So far I’ve been using the “GridView1_dataBound†event to work with the > e.Row(X).Cells(X).Controls(X), but I can’t figure out how to get at the data > that is being applied to the dataView row. > > I think this should be a common task Can anyone point me in the right > direction? > > Thanks! > Bernie > Phillip,
That is really cool. I didn’t even know you could work with logic in the grid code like that. I also over simplified the example. The code that follows is my current flawed attempt. Hopefully the comments will explain the small adjustment I can’t seem to make…. Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 'as long as this is a data row, start working.... If e.Row.RowType = DataControlRowType.DataRow Then ' Retrieve a ref to the checkbox control in the grid. It has the value ‘that I need to work with later ‘**What I’d rather do here is get the value to the field that is populating ‘the checkbox control so I can work with it to set the image button I ‘want to use later. How to do that is what my question boils down to. Dim theCheckBox As CheckBox = CType(e.Row.Cells(6).Controls(0), CheckBox) ‘get a ref to the image button I’m going to work with later Dim theButton As ImageButton = CType(e.Row.Cells(7).Controls(0), ImageButton) ‘***Check if the button is checked, but what I really want to do here is check ‘the value of the field that checked the box in the first place. This works, ‘ but I can only do this if theCheckBox.visible is true. I don’t want that! :-) If theCheckBox.Checked ThentheButton.ImageUrl = "~/images/deletebtn.jpg" ‘set the command argument to use in code when user clicks the ‘the button when the grid is rendered theButton.CommandArgument = "remove-" & GridView1.DataKeys.Item(e.Row.RowIndex).Value.ToString Else ‘set the command argument to use in code when user clicks the ‘the button theButton.CommandArgument = "add-" & GridView1.DataKeys.Item(e.Row.RowIndex).Value.ToString End If End If End Sub Hi Bernie,
You just needed to refer to the DataItem within the provided GridViewRowEventArgs like this: If e.Row.RowType = DataControlRowType.DataRow Then Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView) Dim bval = CType(rowView("BooleanFieldName"), Boolean) 'continue processing based on the retrieved value End If Show quoteHide quote "bernadou" wrote: > Phillip, > That is really cool. I didn’t even know you could work with logic in the > grid code like that. I also over simplified the example. > > The code that follows is my current flawed attempt. Hopefully the comments > will explain the small adjustment I can’t seem to make…. > > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As > System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound > 'as long as this is a data row, start working.... > If e.Row.RowType = DataControlRowType.DataRow Then > ' Retrieve a ref to the checkbox control in the grid. It has > the value > ‘that I need to work with later > ‘**What I’d rather do here is get the value to the field that is populating > ‘the checkbox control so I can work with it to set the image button I > ‘want to use later. How to do that is what my question boils down to. > Dim theCheckBox As CheckBox = CType(e.Row.Cells(6).Controls(0), > CheckBox) > ‘get a ref to the image button I’m going to work with later > Dim theButton As ImageButton = CType(e.Row.Cells(7).Controls(0), > ImageButton) > ‘***Check if the button is checked, but what I really want to do here is > check > ‘the value of the field that checked the box in the first place. This works, > ‘ but I can only do this if theCheckBox.visible is true. I don’t want that! > :-) > If theCheckBox.Checked Then > theButton.ImageUrl = "~/images/deletebtn.jpg" > ‘set the command argument to use in code when user clicks the > ‘the button when the grid is rendered > theButton.CommandArgument = "remove-" & > GridView1.DataKeys.Item(e.Row.RowIndex).Value.ToString > Else > ‘set the command argument to use in code when user clicks the > ‘the button > theButton.CommandArgument = "add-" & > GridView1.DataKeys.Item(e.Row.RowIndex).Value.ToString > End If > End If > > > End Sub > Phillip,
Thanks! That was exactly what I was looking for.... man I looked and looked for that solution. The layers of the GridView onion peel off to reveal more and more all the time. Thanks a ton for your help. B Show quoteHide quote "Phillip Williams" wrote: > Hi Bernie, > > You just needed to refer to the DataItem within the provided > GridViewRowEventArgs like this: > > If e.Row.RowType = DataControlRowType.DataRow Then > Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView) > Dim bval = CType(rowView("BooleanFieldName"), Boolean) > 'continue processing based on the retrieved value > End If > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "bernadou" wrote: > > > Phillip, > > That is really cool. I didn’t even know you could work with logic in the > > grid code like that. I also over simplified the example. > > > > The code that follows is my current flawed attempt. Hopefully the comments > > will explain the small adjustment I can’t seem to make…. > > > > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As > > System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound > > 'as long as this is a data row, start working.... > > If e.Row.RowType = DataControlRowType.DataRow Then > > ' Retrieve a ref to the checkbox control in the grid. It has > > the value > > ‘that I need to work with later > > ‘**What I’d rather do here is get the value to the field that is populating > > ‘the checkbox control so I can work with it to set the image button I > > ‘want to use later. How to do that is what my question boils down to. > > Dim theCheckBox As CheckBox = CType(e.Row.Cells(6).Controls(0), > > CheckBox) > > ‘get a ref to the image button I’m going to work with later > > Dim theButton As ImageButton = CType(e.Row.Cells(7).Controls(0), > > ImageButton) > > ‘***Check if the button is checked, but what I really want to do here is > > check > > ‘the value of the field that checked the box in the first place. This works, > > ‘ but I can only do this if theCheckBox.visible is true. I don’t want that! > > :-) > > If theCheckBox.Checked Then > > theButton.ImageUrl = "~/images/deletebtn.jpg" > > ‘set the command argument to use in code when user clicks the > > ‘the button when the grid is rendered > > theButton.CommandArgument = "remove-" & > > GridView1.DataKeys.Item(e.Row.RowIndex).Value.ToString > > Else > > ‘set the command argument to use in code when user clicks the > > ‘the button > > theButton.CommandArgument = "add-" & > > GridView1.DataKeys.Item(e.Row.RowIndex).Value.ToString > > End If > > End If > > > > > > End Sub > > My pleasure.
Show quoteHide quote "bernadou" wrote: > Phillip, > Thanks! That was exactly what I was looking for.... man I looked and > looked for that solution. The layers of the GridView onion peel off to > reveal more and more all the time. Thanks a ton for your help. > > B > > "Phillip Williams" wrote: > > > Hi Bernie, > > > > You just needed to refer to the DataItem within the provided > > GridViewRowEventArgs like this: > > > > If e.Row.RowType = DataControlRowType.DataRow Then > > Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView) > > Dim bval = CType(rowView("BooleanFieldName"), Boolean) > > 'continue processing based on the retrieved value > > End If > > -- > > HTH, > > Phillip Williams > > http://www.societopia.net > > http://www.webswapp.com > > > > > > "bernadou" wrote: > > > > > Phillip, > > > That is really cool. I didn’t even know you could work with logic in the > > > grid code like that. I also over simplified the example. > > > > > > The code that follows is my current flawed attempt. Hopefully the comments > > > will explain the small adjustment I can’t seem to make…. > > > > > > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As > > > System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound > > > 'as long as this is a data row, start working.... > > > If e.Row.RowType = DataControlRowType.DataRow Then > > > ' Retrieve a ref to the checkbox control in the grid. It has > > > the value > > > ‘that I need to work with later > > > ‘**What I’d rather do here is get the value to the field that is populating > > > ‘the checkbox control so I can work with it to set the image button I > > > ‘want to use later. How to do that is what my question boils down to. > > > Dim theCheckBox As CheckBox = CType(e.Row.Cells(6).Controls(0), > > > CheckBox) > > > ‘get a ref to the image button I’m going to work with later > > > Dim theButton As ImageButton = CType(e.Row.Cells(7).Controls(0), > > > ImageButton) > > > ‘***Check if the button is checked, but what I really want to do here is > > > check > > > ‘the value of the field that checked the box in the first place. This works, > > > ‘ but I can only do this if theCheckBox.visible is true. I don’t want that! > > > :-) > > > If theCheckBox.Checked Then > > > theButton.ImageUrl = "~/images/deletebtn.jpg" > > > ‘set the command argument to use in code when user clicks the > > > ‘the button when the grid is rendered > > > theButton.CommandArgument = "remove-" & > > > GridView1.DataKeys.Item(e.Row.RowIndex).Value.ToString > > > Else > > > ‘set the command argument to use in code when user clicks the > > > ‘the button > > > theButton.CommandArgument = "add-" & > > > GridView1.DataKeys.Item(e.Row.RowIndex).Value.ToString > > > End If > > > End If > > > > > > > > > End Sub > > >
Itemtemplace and many to one dropdown example wanted
SelectedValue which is invalid Error: Multiple controls with the same ID '1' were found. Reports and the ReportViewer GridView Data Binding at runtime Design Time referencing of Properties GUID and data controls Can't Create a multi page report Customise the SiteMapPath Control or use wild card URLs with a custom SiteMapProvider DrawEllipse gives inaccurate results for some radii |
|||||||||||||||||||||||