Solved

GRIDVIEW EDIT - CODE BIND

Posted on 2013-10-24
6
480 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the reference
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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.

728 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

14 Experts available now in Live!

Get 1:1 Help Now