[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

Read Value in Gridview

Hi, I have a gridview which it populated by a db query and submits to a .csv.
I want to be able to look for a value in the gridview in column 1 and if this value is found display a text box. How can I look for a value in the gridview?
Thanks!
Code Below
c#

protected void Button1_Click(object sender, EventArgs e)
        {


            //Get Username
            string[] username = Request.ServerVariables["LOGON_USER"].ToString().Split('\\');
            globalfunctions getAttribute = new globalfunctions();

            string storeEmail = getAttribute.getADattribute(username[1], "mail");

            //append to csv file
            StreamWriter sw = File.AppendText("e:\\results\\t.csv");

            //seperate datagrid to comma seperated values
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string strRowVal = "";
                for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
                {
                    //include template fields
                    GridViewRow row = GridView1.Rows[i];

                   System.Web.UI.WebControls.TextBox Qty = (System.Web.UI.WebControls.TextBox)row.FindControl("TextBox2");
                    String Quant = Qty.Text;
                    System.Web.UI.WebControls.TextBox Cnt = (System.Web.UI.WebControls.TextBox)row.FindControl("TextBox3");
                    String Count = Cnt.Text;

                    //package up with commas
                    if (strRowVal == "")
                    {
                        strRowVal = DateTime.Now + "," + username[1].Remove(3) + "," + completedBy.Text + "," + Layout.Text + "," + "," + Count + "," + Quant + "," + GridView1.Rows[i].Cells[j].Text;
                    }
                    else
                    {
                        strRowVal = strRowVal + "," + GridView1.Rows[i].Cells[j].Text;
                    }
                }
                sw.WriteLine(strRowVal);
            }
            sw.Close();
            Response.Redirect("thanks.aspx");

asp:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site2.Master" AutoEventWireup="true" CodeBehind="T.aspx.cs" Inherits="Templates.WebForm73" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        .style10
        {
            width: 100%;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainPage" runat="server">
    <table class="style10">
        <tr>
            <td>
    <table class="style10">
        <tr>
            <td colspan="3">
                <h1 class="style5">
                    Layout Fixture Audit</h1>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <h5 class="style5">
                    Enter your name and your new layout number, then click on &quot;OK&quot;.
            
                    </h5>
                <h5 class="style5">
                    The details of the layout and fixture list will appear. If the metreage or description do not match your layout , correct the layout number and click on OK again. This will bring up the details for the correct layout.
                    </h5>
                <h5 class="style5">
                    Once you are happy that you have the correct layout on screen, enter your order into the table.  All items must be filled in, even zeroes. Once you have checked the order, click on Submit.
                    </h5>
            </td>
        </tr>
        <tr>
            <td>
                </td>
            <td>
            <form id="webform73" defaultbutton="Button2">
                <b id="completedByText">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><asp:Label 
                    ID="completed" runat="server" style="font-weight: 700" Text="Completed By:"></asp:Label>
&nbsp;<asp:TextBox ID="completedBy" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="completedBy" ErrorMessage="Please enter Your Name"></asp:RequiredFieldValidator>
            </td>
            <td>
               </td>
        </tr>
        <tr>
            <td>
    <asp:SqlDataSource ID="as400" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AS400-Layout %>" 
        ProviderName="<%$ ConnectionStrings:AS400-Layout.ProviderName %>" 
        
        SelectCommand="SELECT PRODUCTION.DSFILESGMF.DSINVMP.IMSKU, PRODUCTION.DSFILESGMF.DSINVMP.IMDESC, PRODUCTION.DSFILESSPO.DSLYSFP.JFFXQT FROM PRODUCTION.DSFILESGMF.DSINVMP, PRODUCTION.DSFILESSPO.DSLYSFP WHERE PRODUCTION.DSFILESGMF.DSINVMP.IMSKU = PRODUCTION.DSFILESSPO.DSLYSFP.JFFSKU AND (PRODUCTION.DSFILESSPO.DSLYSFP.JFLAYN = ?) AND (PRODUCTION.DSFILESGMF.DSINVMP.IMPSA1 = 'XPS') AND (PRODUCTION.DSFILESGMF.DSINVMP.IMPSA2 = 'FIX') ORDER BY PRODUCTION.DSFILESGMF.DSINVMP.IMSKU" 
        >
        <SelectParameters>
            <asp:ControlParameter ControlID="Layout" Name="?" PropertyName="Text" />
        </SelectParameters>
    </asp:SqlDataSource>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:as400LayoutName %>" 
                    ProviderName="<%$ ConnectionStrings:as400LayoutName.ProviderName %>" 
                    
                    SelectCommand="SELECT DISTINCT PRODUCTION.DSFILESLOS.DSLYSDP.JDLAYN, PRODUCTION.DSFILESLOS.DSLYSHP.JHDESC, PRODUCTION.DSFILESLOS.DSLYSHP.JHMTRG FROM PRODUCTION.DSFILESLOS.DSLYSDP, PRODUCTION.DSFILESLOS.DSLYSHP WHERE PRODUCTION.DSFILESLOS.DSLYSDP.JDLAYN = PRODUCTION.DSFILESLOS.DSLYSHP.JHLAYN AND (PRODUCTION.DSFILESLOS.DSLYSDP.JDLAYN = ?)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="Layout" Name="?" PropertyName="Text" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
            <td>
     <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Layout No:</b>
    <asp:TextBox ID="Layout" runat="server"></asp:TextBox>
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="OK" 
                    CausesValidation="False" />
                <br />
                <asp:GridView ID="GridView2" runat="server" CellPadding="4" 
                    DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" 
                    Height="16px" Width="408px" AutoGenerateColumns="False" 
                    style="text-align: center">
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                     <Columns>
         <asp:BoundField DataField="JDLAYN" HeaderText="Layout Number"  />
         <asp:BoundField DataField="JHDESC" HeaderText="Layout Description"  />
         <asp:BoundField DataField="JHMTRG" HeaderText="Metreage"  />
         </Columns>
                </asp:GridView>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td>
               
                &nbsp;&nbsp;</td>
            <td>

    <asp:GridView ID="GridView1" runat="server" DataSourceID="as400" 
        AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <Columns>
         <asp:BoundField DataField="IMSKU" HeaderText="SKU"  />
         <asp:BoundField DataField="IMDESC" HeaderText="Description"  />
         <asp:BoundField DataField="JFFXQT" HeaderText="Required" 
                ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundField>
            <asp:TemplateField HeaderText="Store Count">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Height="16px" Width="97px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Order Quantity" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Height="16px" 
                         Width="97px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="TextBox2" ErrorMessage="You must fill ALL fields!"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
           
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
            </td>
            <td>
                </td>
        </tr>
        <tr>
            <td>
               </td>
            <td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" 
        style="height: 26px" Text="Submit" onclick="Button1_Click" />
            </td>
            <td>
               </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
</asp:Content>

Open in new window

0
Gaz124
Asked:
Gaz124
  • 5
  • 5
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Assuming your cell just contains plain text rather than a control you can simply loop through like:
foreach(GridViewRow row in yourGridView)
{
    if (row.Cells[0].Text == "some value")
    {
         // do something
    }
}

Open in new window

0
 
Gaz124Author Commented:
hi carl,

Ive tried that but get an error saying foreach cannot operate on variables of gridview type, because  system.web.ui.webcontrols.gridview does not contain a public definition of 'Get Emnumerator'


protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1)
            {
                if (row.Cells[0].Text == "some value")
                {
                    // do something
                }
            }

            //Get Username
            string[] username = Request.ServerVariables["LOGON_USER"].ToString().Split('\\');
            globalfunctions getAttribute = new globalfunctions();

            string storeEmail = getAttribute.getADattribute(username[1], "mail");

            //append to csv file
            StreamWriter sw = File.AppendText("e:\\results\\t.csv");

            //seperate datagrid to comma seperated values
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string strRowVal = "";
                for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
                {
                    //include template fields
                    GridViewRow row = GridView1.Rows[i];

                   System.Web.UI.WebControls.TextBox Qty = (System.Web.UI.WebControls.TextBox)row.FindControl("TextBox2");
                    String Quant = Qty.Text;
                    System.Web.UI.WebControls.TextBox Cnt = (System.Web.UI.WebControls.TextBox)row.FindControl("TextBox3");
                    String Count = Cnt.Text;        

                    //package up with commas
                    if (strRowVal == "")
                    {
                        strRowVal = DateTime.Now + "," + username[1].Remove(3) + "," + completedBy.Text + "," + Layout.Text + "," + "," + Count + "," + Quant + "," + GridView1.Rows[i].Cells[j].Text;
                    }
                    else
                    {
                        strRowVal = strRowVal + "," + GridView1.Rows[i].Cells[j].Text;
                    }
                }
                sw.WriteLine(strRowVal);
            }
            sw.Close();
            Response.Redirect("thanks.aspx");

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Ooops, sorry, that should have said:
foreach(GridViewRow row in yourGridView.Rows)

Open in new window

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!

 
kovilpattiBaluCommented:
hi,
GridViewRow row = GridView1.Rows[0];
TextBox1.Text = row.Cells[2].Text;


0
 
Gaz124Author Commented:
Hi Carl,

Thanks for the response but it doesn't seeem to be working. The textbox is always visible.
protected void Button1_Click(object sender, EventArgs e)
        {
           
              //Get Username
            string[] username = Request.ServerVariables["LOGON_USER"].ToString().Split('\\');
            globalfunctions getAttribute = new globalfunctions();

            string storeEmail = getAttribute.getADattribute(username[1], "mail");

            //append to csv file
            StreamWriter sw = File.AppendText("e:\\results\\t.csv");

            //seperate datagrid to comma seperated values
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string strRowVal = "";
                for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
                {
        
                        //include template fields
                        GridViewRow row = GridView1.Rows[i];

                        System.Web.UI.WebControls.TextBox Qty = (System.Web.UI.WebControls.TextBox)row.FindControl("TextBox2");
                        String Quant = Qty.Text;
                        System.Web.UI.WebControls.TextBox Cnt = (System.Web.UI.WebControls.TextBox)row.FindControl("TextBox3");
                        String Count = Cnt.Text;

                        //package up with commas
                        if (strRowVal == "")
                        {
                            strRowVal = DateTime.Now + "," + username[1].Remove(3) + "," + completedBy.Text + "," + Layout.Text + "," + "," + Count + "," + Quant + "," + GridView1.Rows[i].Cells[j].Text;
                        }
                        else
                        {
                            strRowVal = strRowVal + "," + GridView1.Rows[i].Cells[j].Text;
                        }
                        foreach (GridViewRow row1 in GridView1.Rows)
                        if (row.Cells[0].Text == ("129944"))
                        {

                            TextBox4.Visible = true;

                        }

                        else
                        {

                            TextBox4.Visible = false;

                    }

                    sw.WriteLine(strRowVal);
                }
                sw.Close();
                Response.Redirect("thanks.aspx");

            }
        }

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Are those definitely the right names for your textboxes? Are you not finding the controls or are their values simply coming back empty?
0
 
Gaz124Author Commented:
No they work 100% and I get these values back into the .csv great.

The column I want to look for the value in is the first Databound column, 'SKU' - <asp:BoundField DataField="IMSKU" HeaderText="SKU"  />

if that column contains a certain SKU e.g. 129944 then I want textbox 4 to become visable

Thanks
0
 
Carl TawnSystems and Integration DeveloperCommented:
OK, well you're already looping through the rows in the grid so you don't need the extra for loop. As long as the SKU column doesn't contain any controls then the following should check its content:
           //seperate datagrid to comma seperated values
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string strRowVal = "";
                for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
                {
        
                        //include template fields
                        GridViewRow row = GridView1.Rows[i];

                        if (row.Cells[0].Text == "1234")
                        {
                              TextBox4.Visible = true;
                        }
                    
                         // rest of your code
                 }
            }

Open in new window

Where does TextBox4 sit in your page? Is it in the Grid?
0
 
Gaz124Author Commented:
Sorry doesn't work, textbox4 remains hidden
0
 
Carl TawnSystems and Integration DeveloperCommented:
As I said before, where does TextBox4 sit releative to your grid/row? Have you actually stepped through your code with the debugger to check what route the code is taking and what value, if any, is coming from the grid cell?
0
 
Gaz124Author Commented:
Sorry textbox 4 is on the main page, not in the grid, ill try debuggin now
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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