Solved

GRIDVIEW EDIT - CODE BIND

Posted on 2013-10-24
6
486 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 61
Install Problem 13 31
Keyboard 2 33
VB.NET - Error - Object not set in Module 4 33
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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