2008-09-29 123 views

回答

3
protected void Page_Load(object sender, EventArgs e) 
{ 
    DataGrid dg = new DataGrid(); 

    dg.GridLines = GridLines.Both; 

    dg.Columns.Add(new ButtonColumn { 
    CommandName = "add", 
    HeaderText = "Event Details", 
    Text = "Details", 
    ButtonType = ButtonColumnType.PushButton 
    }); 

    dg.DataSource = getDataTable(); 
    dg.DataBind(); 

    dg.ItemCommand += new DataGridCommandEventHandler(dg_ItemCommand); 

    pnlMain.Controls.Add(dg); 
} 

protected void dg_ItemCommand(object source, DataGridCommandEventArgs e) 
{ 
    if (e.CommandName == "add") 
    { 
    throw new Exception("add it!"); 
    } 
} 

protected DataTable getDataTable() 
{ 
    // returns your data table 
} 
0

這裏就是我創建數據網格:

System.Web.UI.WebControls.DataGrid Datagridtest =新System.Web.UI.WebControls.DataGrid();

 Datagridtest.Width = 600; 
     Datagridtest.GridLines = GridLines.Both; 
     Datagridtest.CellPadding = 1; 

     ButtonColumn bc = new ButtonColumn(); 
     bc.CommandName = "add"; 
     bc.HeaderText = "Event Details"; 
     bc.Text = "Details"; 
     bc.ButtonType = System.Web.UI.WebControls.ButtonColumnType.PushButton; 
     Datagridtest.Columns.Add(bc); 
     PlaceHolder1.Controls.Add(Datagridtest); 

     Datagridtest.DataSource = dt; 
     Datagridtest.DataBind(); 

這裏是我試圖使用事件:

保護無效Datagridtest_ItemCommand(對象源,DataGridCommandEventArgs E) { .... }

思想,可以幫助因爲我似乎無法捕捉到這個事件。

相關問題