Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a templateItem to GridVIew Dynamically

Status
Not open for further replies.
Jul 28, 2011
167
NG
Hi all, I'm working on with a gridview and I want to add a radiobutton to each row. The value of radiobutton on each row should be the code from database.
Currently, in my aspx I have
Code:
<asp:GridView ID="GridView1" runat="server" >
 <Columns>
      <asp:TemplateField HeaderText="">
        <ItemTemplate>
          <input name="codeTitleName" type="radio" value='<%# Eval("loc_code") %>' onclick="doJavascript()" />
        </ItemTemplate>
      </asp:TemplateField>
  </Columns>
 </asp:GridView>
and my aspx.vb
Code:
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        fillStatesControls("Select [loc_code], [loc_descr] From " & Request.QueryString("q"), "code")
        Me.DataBind()
    End Sub

    Protected Sub fillStatesControls(ByRef theQuery As String)

        Dim myda As SqlDataAdapter
        Dim ds As DataSet
        Dim mycn As New SqlConnection(connectionString)
        myda = New SqlDataAdapter(theQuery, mycn)

        ds = New DataSet
        myda.Fill(ds, "theTables")

        Dim ds_source As DataSet = ds 'Previously Retrieved Dataset

        Dim code As New BoundField
        code.HeaderText = "code"
        code.DataField = "loc_code"
        GridView1.Columns.Add(code)

        Dim desc As New BoundField
        desc.HeaderText = "Description"
        desc.DataField = "loc_descr"
        GridView1.Columns.Add(desc)

        GridView1.Visible = True
        GridView1.DataSource = ds_source

    End Sub
This works perfect. However I need to be able to pass different queries with different codes then I tried this
Code:
<asp:GridView ID="GridView1" runat="server" >
 <Columns>
      <asp:TemplateField HeaderText="">
        <ItemTemplate>
          <input name="codeTitleName" type="radio" value='<%# Eval(codeTitle) %>' onclick="doJavascript()" />
        </ItemTemplate>
      </asp:TemplateField>
  </Columns>
 </asp:GridView>
and
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        fillStatesControls("Select [loc_code], [loc_descr] From " & Request.QueryString("q"), "code", "Description", "loc_code", "loc_descr")
        Me.DataBind()
    End Sub

    Protected Sub fillStatesControls(ByRef theQuery As String, ByVal codeTitle As String, ByVal descTitle As String, _
                                     ByVal codeDataField As String, ByVal descDataField As String)


        Dim myda As SqlDataAdapter
        Dim ds As DataSet
        Dim mycn As New SqlConnection(connectionString)
        myda = New SqlDataAdapter(theQuery, mycn)

        ds = New DataSet
        myda.Fill(ds, "theTables")

        Dim ds_source As DataSet = ds 'Previously Retrieved Dataset

        Dim radio as

        Dim code As New BoundField
        code.HeaderText = codeTitle
        code.DataField = codeDataField
        GridView1.Columns.Add(code)

        Dim desc As New BoundField
        desc.HeaderText = descTitle
        desc.DataField = descDataField
        GridView1.Columns.Add(desc)

        GridView1.Visible = True
        GridView1.DataSource = ds_source

    End Sub
and it fails.
So I'm thinking of putting the TemplateField dynamically, any ideas?

____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?

Think about it.
 
the first problem is you are using a gridview to generate radio buttons. just use a RadioButtonList control. that will be much cleaner.

the second issue is loading the data.
if the results sets have the same schema, then all you need to do is execute different queries and bind the results to the radio buttons. if the results sets are completely different, it would be better to setup different controls for the various data sources. you can then show/hide the appropriate UI elements.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks Jason Meckley,
I was able to overcome the fall by using a cheap cheat in the query string thus:

fillStatesControls("Select [loc_code][theCode], [loc_descr] From " & Request.QueryString("q"), "code", "Description", "theCode", "loc_descr")
then for my binding, I did:
Code:
<asp:GridView ID="GridView1" runat="server" >
 <Columns>
      <asp:TemplateField HeaderText="">
        <ItemTemplate>
          <input name="codeTitleName" type="radio" value='<%# Eval("theCode") %>' onclick="doJavascript()" />
        </ItemTemplate>
      </asp:TemplateField>
  </Columns>
 </asp:GridView>

Worked pretty cool for all, by just binding all code to theCode

____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?

Think about it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top