[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Read Value in Gridview

Posted on 2011-03-16
11
Medium Priority
?
540 Views
Last Modified: 2012-05-11
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
Comment
Question by:Gaz124
  • 5
  • 5
11 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 35146933
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
 

Author Comment

by:Gaz124
ID: 35147029
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35147061
Ooops, sorry, that should have said:
foreach(GridViewRow row in yourGridView.Rows)

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 7

Expert Comment

by:kovilpattiBalu
ID: 35147178
hi,
GridViewRow row = GridView1.Rows[0];
TextBox1.Text = row.Cells[2].Text;


0
 

Author Comment

by:Gaz124
ID: 35147323
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35147755
Are those definitely the right names for your textboxes? Are you not finding the controls or are their values simply coming back empty?
0
 

Author Comment

by:Gaz124
ID: 35147813
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35147858
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
 

Author Comment

by:Gaz124
ID: 35148252
Sorry doesn't work, textbox4 remains hidden
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35148288
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
 

Author Comment

by:Gaz124
ID: 35148404
Sorry textbox 4 is on the main page, not in the grid, ill try debuggin now
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question