• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

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 :))
0
Mike-Po
Asked:
Mike-Po
  • 2
1 Solution
 
ddayx10Commented:
You are mixing events of a Gridview with those of a DataGrid.

>> protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)<<

A DataGrid does not have an OnRowDataBound event. You want to look at the OnItemDataBound/ItemDataBound instead.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.aspx

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

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now