• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

How do I get the value of one of the fields in my Gridview control

How do I get the value of one of the fields in my Gridview Control (wsID) and use it in the same row  in a template field variable  for use in a button control to to query the db


//*** this is where I need help how do I get the value of the wsID field into this variable?



        string workshopID;
0
bmanmike39
Asked:
bmanmike39
  • 10
  • 10
  • 2
1 Solution
 
Bob LearnedCommented:
That is a pretty vague question, since it doesn't explain where you need to get a reference to the field.  Are you talking about the RowDataBound event handler?  How are the columns defined in the HTML for the GridView?
0
 
sdrouinsCommented:
could you show use your code please!
0
 
sdrouinsCommented:
maybe something like this;

    Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        Dim variable As String
        'depend on wich row your data is
        variable = e.Row.Cells(0).Text
        CType(e.Row.Cells(1).FindControl("YourButtonid"), Button).CommandArgument = variable
    End Sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bmanmike39Author Commented:
Gridview control

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="wsInID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="wsInID" HeaderText="wsInID" InsertVisible="False"
                    ReadOnly="True" SortExpression="wsInID" />
                <asp:BoundField DataField="wsID" HeaderText="wsID" SortExpression="wsID" />
                <asp:BoundField DataField="InstID" HeaderText="InstID"
                    SortExpression="InstID" />
                <asp:BoundField DataField="InstName" HeaderText="InstName"
                    SortExpression="InstName" />
                <asp:TemplateField HeaderText="query Db">
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                            Text="change value" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>



Code BH:

//*** this is where I need help, how do I get the value of the wsID field into this variable?

        string workshopID;

string query = " SELECT  wsInstructors.wsInID, wsInstructors.wsID, wsInstructors.InstID, Instructors.InstName + ' ' + Instructors.LName AS Name   FROM  wsInstructors INNER JOIN  Instructors ON wsInstructors.InstID = Instructors.instID WHERE (wsID = '" + workshop + "')";

0
 
Bob LearnedCommented:
That sounds like you have an SqlDataSource, and you need a parameter for the UpdateCommand...
0
 
bmanmike39Author Commented:
I just want to get the wsID value in the Gridview into a Variable called worshopID
0
 
Bob LearnedCommented:
Where is that query code in the code-behind?  You would have to get that value from a GridView row...which row?  That looks like a primary key, so if you get the GridView.DataKeyNames property, you should be able to get the value from the row's DataKeys property.
0
 
bmanmike39Author Commented:
Yes the code is in code behind but its not the datakey its a field name wsID



This is what i have for code:

DataRow myRow = myds.Tables[0].Rows[0];
        string whsID = myRow[myds.Tables[0].Columns["wsID"]].ToString();
        Label1.Text = whsID.ToString();
0
 
Bob LearnedCommented:
Do you need to get that information from the first GridViewRow?  Are the values for 'wsID' unique?
0
 
bmanmike39Author Commented:
each row  I have a query that uses the value to display the name of the Instructor who is associated with that workshop. in a label in the template field in the grid.
0
 
Bob LearnedCommented:
Are you looking for something like this?

foreach (GridViewRow row in this.GridView1.Rows)
{
    Label label = (Label)row.FindControl("Label1");
    string labelText = label.Text;
}
0
 
bmanmike39Author Commented:
i tried this but get no return value
0
 
Bob LearnedCommented:
Are you saying that you can find the Label, but it doesn't have any value?  If so, then you need to give me some more detail, otherwise it would be very difficult to help you.
0
 
bmanmike39Author Commented:
I get now return value in the lable

This is the button in my gridview

protected void Button1_Click(object sender, EventArgs e)
    {
         foreach (GridViewRow row in this.GridView1.Rows)
        {
            Label label = (Label)row.FindControl("Label1");
            string labelText = label.Text;

            Label3.Text = labelText;
        }
   
    }

This is my Gridview control:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="wsInID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="wsInID" HeaderText="wsInID" InsertVisible="False"
                    ReadOnly="True" SortExpression="wsInID" />
                <asp:BoundField DataField="wsID" HeaderText="wsID" SortExpression="wsID" />
                <asp:BoundField DataField="InstID" HeaderText="InstID"
                    SortExpression="InstID" />
                <asp:TemplateField HeaderText="get var field">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="grid test">
                    <ItemTemplate>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
                            ConnectionString="<%$ ConnectionStrings:manConnectionString %>"
                            SelectCommand="SELECT * FROM [Instructors] WHERE ([instID] = @instID)">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="Label2" Name="instID" PropertyName="Text"
                                    Type="Int32" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
0
 
Bob LearnedCommented:
What was wrong before?
0
 
bmanmike39Author Commented:
I didn't get a return value.  no data in the labletext var
0
 
bmanmike39Author Commented:
The Gridview returns the following values

Columns
(1)Wsid,  (2)wsDate,   (3)(Template column)variable wsIns


I need the wsId number for the template columb var wsIns because there is a Gridview control and sqlDataSourse control in the Template column the selects all the instructors assigned to that workshop, and displays them.  It draws this information from a table that associate each workshop with specific instructors.

I hope this clarifys what Im trying to do.
0
 
Bob LearnedCommented:
Are you saying that you are trying to get a value from the wsID BoundField?

 <asp:BoundField DataField="wsID" HeaderText="wsID" SortExpression="wsID" />
0
 
bmanmike39Author Commented:
yes
0
 
Bob LearnedCommented:
If you were not using the SqlDataSource, then you could get the data from the GridViewRow.DataItem.  But, since you are binding to an SqlDataSource, then you don't have access to that property.  I would suggest doing something like this:

        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

        foreach (DataRowView drv in dv)
        {
            string wsID = drv["wsID"].ToString();
        }
0
 
bmanmike39Author Commented:
when i step into it i see the value is in the variable, but when i try to pass it into my connection string i get the following error :

"Multiple controls with the same ID 'Label0' were found. FindControl requires that controls have unique IDs."

I tested the  connection string and it works, only when i try to pass in the wsID I have the problem.
0
 
Bob LearnedCommented:
Can you show me what you tried?  
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 10
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now