Link to home
Start Free TrialLog in
Avatar of bmanmike39
bmanmike39

asked on

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;
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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?
Avatar of sdrouins
sdrouins

could you show use your code please!
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
Avatar of bmanmike39

ASKER

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 + "')";

That sounds like you have an SqlDataSource, and you need a parameter for the UpdateCommand...
I just want to get the wsID value in the Gridview into a Variable called worshopID
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.
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();
Do you need to get that information from the first GridViewRow?  Are the values for 'wsID' unique?
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.
Are you looking for something like this?

foreach (GridViewRow row in this.GridView1.Rows)
{
    Label label = (Label)row.FindControl("Label1");
    string labelText = label.Text;
}
i tried this but get no return value
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.
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>
What was wrong before?
I didn't get a return value.  no data in the labletext var
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.
Are you saying that you are trying to get a value from the wsID BoundField?

 <asp:BoundField DataField="wsID" HeaderText="wsID" SortExpression="wsID" />
yes
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Can you show me what you tried?