Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

GRIDVIEW EDIT - CODE BIND

Posted on 2013-10-24
6
489 Views
Last Modified: 2013-12-17
I am having issues with Gridview1 and my code behind.    When I convert field "Status1" to a template field in order to utilize a dropdown list my code behind will not fire.   The code behind Onrowdatabound event fires normal when it is left as the standard textbox field and no template insertion is utilized in the gridview when selecting edit.

Can someone assist?

<asp:GridView ID="GridView1" onrowdatabound="GridView1_RowDataBound" 
                        runat="server" EnableModelValidation="True" 
                        Width="375px" AllowSorting="True" AutoGenerateColumns="False" 
                        DataKeyNames="ID" DataSourceID="SqlDataSource1" 
                        ForeColor="White" BackColor="#006699" BorderColor="Black" 
                        style="font-size: medium">
                        <Columns>
                            <asp:CommandField DeleteText="Del" ShowDeleteButton="True" 
                                ShowEditButton="True">
                            <HeaderStyle HorizontalAlign="Left" Width="25px" />
                            <ItemStyle HorizontalAlign="Left" Width="25px" ForeColor="Black" />
                            </asp:CommandField>

                        <asp:templatefield HeaderText="No.">
&#9;&#9;&#9;&#9;&#9;&#9;&#9;<ItemTemplate>
&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;<%# Container.DataItemIndex + 1 %>
&#9;&#9;&#9;&#9;&#9;&#9;&#9;</ItemTemplate>
&#9;&#9;&#9;&#9;&#9;&#9;&#9;<HeaderStyle Font-Underline="True" Width="25px" HorizontalAlign="Center" 
                                ForeColor="White" />
&#9;&#9;&#9;&#9;&#9;&#9;&#9;<ItemStyle HorizontalAlign="Center" Wrap="False" ForeColor="Black" 
                                Width="25px" />
&#9;&#9;&#9;&#9;&#9;&#9;</asp:templatefield>
                        
                            <asp:TemplateField HeaderText="Ship" SortExpression="Ship1">
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Ship1") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:DropDownList ID="DropDownList1" runat="server" 
                                        DataSourceID="SqlDataSource111" DataTextField="Ship" DataValueField="Ship" 
                                        Height="22px" SelectedValue='<%# Bind("Ship1") %>' Width="92px">
                                    </asp:DropDownList>
                                    <asp:SqlDataSource ID="SqlDataSource111" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings: %>" 
                                        SelectCommand="SELECT DISTINCT [Ship] FROM [DropListData] ORDER BY [Ship]"></asp:SqlDataSource>
                                </EditItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" Width="45px" />
                                <ItemStyle ForeColor="Black" HorizontalAlign="Center" Width="45px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="DepTime" SortExpression="DepTime1">
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("DepTime1") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("DepTime1") %>' 
                                        Width="88px"></asp:TextBox>
                                    <br />
                                    <asp:RegularExpressionValidator ID="Validator4" runat="server" 
                                        ControlToValidate="TextBox3" CssClass="style14" 
                                        ErrorMessage="(24:00 format)" Font-Bold="False" Font-Size="Small" 
                                        ValidationExpression="^(20|21|22|23|[01]\d|\d)(([:][0-5]\d){1,2})$" />
                                </EditItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" Width="45px" />
                                <ItemStyle ForeColor="Black" HorizontalAlign="Center" Width="45px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Gate" SortExpression="GT">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Height="19px" 
                                        Text='<%# Bind("GT") %>' Width="72px"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("GT") %>'></asp:Label>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" Width="45px" />
                                <ItemStyle ForeColor="Black" HorizontalAlign="Center" Width="45px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Dest City" SortExpression="Dest1">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="DropDownList3" runat="server" 
                                        DataSourceID="SqlDataSource21" DataTextField="Station" DataValueField="Station" 
                                        Height="22px" SelectedValue='<%# Bind("Dest1") %>' Width="104px">
                                    </asp:DropDownList>
                                    <asp:SqlDataSource ID="SqlDataSource21" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings: %>" 
                                        SelectCommand="SELECT [Station] FROM [DropListData]"></asp:SqlDataSource>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Dest1") %>'></asp:Label>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" Width="45px" />
                                <ItemStyle HorizontalAlign="Center" Width="45px" ForeColor="Black" />
                            </asp:TemplateField>
                        
                            <asp:TemplateField HeaderText="Acft Status" SortExpression="Status1">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="DropDownList6" runat="server" AutoPostBack="True" 
                                        Height="22px" SelectedValue='<%# Bind("Status1") %>' Width="82px">
                                        <asp:ListItem></asp:ListItem>
                                        <asp:ListItem>RDY</asp:ListItem>
                                        <asp:ListItem>BOB</asp:ListItem>
                                        <asp:ListItem>INSP</asp:ListItem>
                                        <asp:ListItem>WKG</asp:ListItem>
                                        <asp:ListItem>AOG</asp:ListItem>
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Status1") %>'></asp:Label>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" Width="45px" />
                                <ItemStyle ForeColor="Black" HorizontalAlign="Center" Width="45px" />
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

Open in new window





public partial class BigBoardDisp : System.Web.UI.Page
{

    protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            if (e.Row.Cells[6].Text == "AOG" || e.Row.Cells[6].Equals(DBNull.Value))
            {
                e.Row.BackColor = System.Drawing.Color.Red;
            }

            else if (e.Row.Cells[6].Text == "RDY")
            {
                e.Row.BackColor = System.Drawing.Color.Aqua;
            }

            else if (e.Row.Cells[6].Text == "WKG")
            {
                e.Row.BackColor = System.Drawing.Color.LightSkyBlue;
            }

            else if (e.Row.Cells[6].Text == "INSP")
            {
                e.Row.BackColor = System.Drawing.Color.Yellow;
            }

            else if (e.Row.Cells[6].Text == "BOB")
            {
                e.Row.BackColor = System.Drawing.Color.Lime;
            }

    }

Open in new window

0
Comment
Question by:BOEING39
  • 2
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 39602212
It's difficult for us to see what's going on; you posted one piece of the code, and even that piece has things that we cannot run, such as connection. Either explain the problem assuming that we have no idea about your application, or upload whole solution together with the data source it's using, so we can reproduce.

The event "Onrowdatabound " fires on each row of the gridview, as the name suggests, so it does not matter what controls are within the row.
0
 

Author Comment

by:BOEING39
ID: 39603171
Ok, I have attached the entire code.   An item to note, As soon as I identify or change the "Status1" column to a Template field, I am losing all my code behind formatting which is setup to change the row color based on the "Status1" value.   If I leave the field un-modified, or just a TextBox field the formatting works or the code behind fires.  I have modified Gridview1 in the attached and left Gridview2 un-modified so you can see what is occurring.  However, both "Status1" and "Status2" columns need to have a dropdown box with selectable values that can be changed by the user using the EDIT function.
BigBoardDisp.aspx
BigBoardDisp.aspx.cs
0
 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 500 total points
ID: 39604195
It's not entire code, entire code would be the whole solution complete with the database file the gridview takes its data from. We should be able to open it, and actually run it and see how it behaves.

Here's an example of sample solution together with its datasource.

It's understood that your real database may contain real data you don't want to share, but for the purpose of showing the problem on e-e, you may want to isolate it to simple separate solution that would have only the elements necessary to show the problem.
0
 

Author Closing Comment

by:BOEING39
ID: 39637283
Thanks for the reference
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

839 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