Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, recently started learning asp.net and already have a problem that can't myself decide. I need to create arbitrary number a GridView which output information from DataBase and propose editing it ("edit" button). Code crashed when I click in button "edit" with that error "The GridView 'ctl02' fired event RowEditing which wasn't handled." When I create one static control everything is working fine, but I need dynamically generate controls.
C#
public partial class _Default : System.Web.UI.Page
{
    private SqlConnection Conn;
    GridView Grid;

    override protected void OnInit(EventArgs e)
    {
        Conn = new SqlConnection(
            ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString
            );
        Conn.Open();

        Grid = new GridView();
        Grid.AutoGenerateColumns = false;

        CommandField Command = new CommandField();
        Command.ShowEditButton = true;
        Grid.Columns.Add(Command);

        BoundField Boundf = new BoundField();
        Boundf.DataField = "FamilyName";
        Grid.Columns.Add(Boundf);

        SqlDataSource DataSource = new SqlDataSource(
            ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString,
            "SEL ECT FamilyName FR OM Samples");

        Grid.DataSource = DataSource;
        Grid.DataBind();

        this.Form.Controls.Add(this.Grid);

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.DataBind();
    }


This code work fine

C#
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
 <head  runat="server">
    <title>Interface<title>
  <head>
  <body>
    <fo rm id="Form"  runat="server">
    <div>
        <asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" CellPadding="3" DataSourceID="SqlDataSourceMyDB">
            <Columns>
                <asp:BoundField DataField="NumberInFamily" HeaderText="NumberInFamily" SortExpression="NumberInFamily" />
                <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:CommandField ShowEditButton="True" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSourceMyDB" runat="server" ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>"
            SelectCommand="SELECT FamilyName Id FR OM Samples">
        </asp:SqlDataSource>
    </div>
    <form>
  <body>
  <html>

Please help me.
P.S. excuse me for my language
Posted

1 solution

Read this[^] about creating controls dynamically. Should get you started.
 
Share this answer
 
Comments
arthur1987 27-Jun-12 16:40pm    
Added GridView.Id property befor added it to the form, but this don't help me(
R. Giskard Reventlov 27-Jun-12 17:13pm    
Look at ViewState.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900