Solved

DataGrid Template Column

Posted on 2011-03-09
5
489 Views
Last Modified: 2013-12-17
Hi,

I have a DataGrid with a TemplateColumn which contains a textbox. I want to be able to bring back the value of the textbox to a string. Can anyone help me do this?

Thanks
0
Comment
Question by:Gaz124
  • 3
  • 2
5 Comments
 

Author Comment

by:Gaz124
ID: 35082002

I have tried the following code with little success!
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");

            DataGridItem item = (sender as Button).NamingContainer as DataGridItem;
            string Name = ((TextBox)(item["QTY"].FindControl("TextBox2"))).Text;

            TextBox Qty = (TextBox)((Button)sender).NamingContainer.FindControl("TextBox2");

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35090455
It looks like you are trying to use FindControl with the item, but I don't believe that you have the right reference, but it is difficult to be sure, without seeing the HTML declaration for the DataGrid.
0
 

Author Comment

by:Gaz124
ID: 35093274
Ok, heres the code asp and c#. I can get every other part of the datagrid to my .csv but not the template column, Frustrating!!


<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="Qty"> 
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </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>


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\\decdiylayout.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++)
            {
                if (strRowVal == "")
                {
                    strRowVal = DateTime.Now + "," + username[1].Remove(3)+ "," + Layout.Text + "," + GridView1.Rows[i].Cells[j].Text;
                }
                else
                {
                    strRowVal =  strRowVal + "," + GridView1.Rows[i].Cells[j].Text;
                }
            }
            sw.WriteLine(strRowVal);
        }
        sw.Close();

Open in new window

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 35099839
DataGrid:  1.1
GridView:  >=2.0

You need to call FindControl on the GridViewRow, as in this example:

GridViewRow row = this.GridView1.Rows[0];
TextBox textBox = (TextBox)row.FindControl("TextBox1");

Open in new window

0
 

Author Comment

by:Gaz124
ID: 35107937
WOW TheLearnedOne Thats Great!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now