Link to home
Start Free TrialLog in
Avatar of Mike-Po
Mike-Po

asked on

OnRowDataBound not being fired in my DataGrid

Hello,

I have a DG that I am missing something to get the OnRowDataBound to fire so that I can populate a dropdown list. I'm sure there are errors that will happen after, but even with debug the OnRowDataBound never seems to be fired to even check the IF statement. I'm sure its something blatently simple. I'm just at a loss. Its been a few years since I've done anything with ASP.NET and am back to nubblet stage.

<asp:DataGrid ID="myDG" runat="server" 
            AutoGenerateColumns="false"
            BorderColor="Black"
            BorderWidth="1"
            CellPadding="1"
            ShowFooter="true"
            OnEditCommand="Grid_EditCommand"
            OnCancelCommand="Grid_CancelCommand"
            OnRowDataBound="Grid_RowDataBound">
            
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
            <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
            <EditItemStyle BackColor="#FFCC66" Font-Bold="true" ForeColor="Navy" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" Mode="NumericPages" /> 
            <AlternatingItemStyle BackColor="White" /> 
            <ItemStyle BackColor="#FFFBD6" ForeColor="#333333" /> 
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 

            <Columns>
                <asp:EditCommandColumn 
                    EditText="Edit" 
                    CancelText="Cancel" 
                    UpdateText="Update" 
                    HeaderText="Do Stuff" />

                <asp:TemplateColumn HeaderText="Server Name">
                    <ItemTemplate>
                        <asp:Label id="lblone" runat="server" Text='<%# Bind("Server_Name")%>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox id="txtone" Text='<%# Bind("Server_Name")%>' runat="server" />
                    </EditItemTemplate>
                </asp:TemplateColumn>

                <asp:TemplateColumn HeaderText="Server IP">
                    <ItemTemplate>
                        <asp:Label id="lbltwo" runat="server" Text='<%# Bind("Server_IP")%>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DDL1" runat="server" DataTextField='<%# Bind("Server_IP") %>' DataValueField='<%# Bind("Server_IP") %>' />
                    </EditItemTemplate>
                </asp:TemplateColumn>
            </Columns>
            
        </asp:DataGrid>

Open in new window


with the codebehind of
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//Added by me
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class DGTest : System.Web.UI.Page
{
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand();
    SqlConnection con;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }

    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
        {
            DropDownList cmbType = (DropDownList)e.Row.FindControl("DDL1");
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["MSGLabsAssetsConnectionString"].ConnectionString);
            cmd.CommandText = "select top 2 Server_IP from fulllist";
            cmd.Connection = con;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            con.Open();
            cmd.ExecuteNonQuery();
            cmbType.DataSource = ds;
            cmbType.DataTextField = "Server_IP";
            cmbType.DataValueField = "Server_IP";
            cmbType.DataBind();
            con.Close();
        }
    }

    public void BindData()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["MSGLabsAssetsConnectionString"].ConnectionString);
        cmd.CommandText = "select top 2 * from fulllist";
        cmd.Connection = con;
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        con.Open();
        cmd.ExecuteNonQuery();
        myDG.DataSource = ds;
        myDG.DataBind();
        con.Close();
    }

    protected void Grid_EditCommand(object source, DataGridCommandEventArgs e)
    {
        myDG.EditItemIndex = e.Item.ItemIndex;
        BindData();
    }

    protected void Grid_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        myDG.EditItemIndex = -1;
        BindData();
    }
}

Open in new window



(Its all in longhand to debug, dont judge to much :))
ASKER CERTIFIED SOLUTION
Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike-Po
Mike-Po

ASKER

Glaring mistake pointed out after hours of looking at the same rewritten code. Much appreciated.
It's happened to us all :)