使用 DataGrid的  OnEditCommand=""
                 OnCancelCommand=""
                 OnUpdateCommand=""-------努力学习 不断实践 虚心讨教--------

解决方案 »

  1.   

    <asp:DataGrid  DataKeyField="LotteryID" id="classpicsDG" runat="server" Width="100%" AutoGenerateColumns="False" CellPadding="4"
    BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#3366CC">
    <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
    <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" Wrap="False" HorizontalAlign="Center" ForeColor="#CCCCFF" VerticalAlign="Middle"
    BackColor="#003399"></HeaderStyle>
    <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
    <Columns>
    <asp:BoundColumn DataField="LotteryID" SortExpression="LotteryID" ReadOnly="True" HeaderText="序号">
    <HeaderStyle Width="40px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="HomeTeam" SortExpression="HomeTeam" ReadOnly="True" HeaderText="主队">
    <HeaderStyle Width="200px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="GuestTeam" SortExpression="GuestTeam" ReadOnly="True" HeaderText="客队">
    <HeaderStyle Width="200px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="FirstSelect" SortExpression="FirstSelect" HeaderText="首选">
    <HeaderStyle Width="40px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="FirstConfidence" SortExpression="FirstConfidence" HeaderText="信心">
    <HeaderStyle Width="40px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="SecondSelect" SortExpression="SecondSelect" HeaderText="次选">
    <HeaderStyle Width="40px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="SecondConfidence" SortExpression="SecondConfidence" HeaderText="信心">
    <HeaderStyle Width="40px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="LastSelect" SortExpression="LastSelect" HeaderText="末选">
    <HeaderStyle Width="40px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="LastConfidence" SortExpression="LastConfidence" HeaderText="信心">
    <HeaderStyle Width="40px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
    </Columns>
    <PagerStyle Visible="False" HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid>
    // .cs
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.lnkOK.Click += new System.EventHandler(this.lnkOK_Click);
    this.Load += new System.EventHandler(this.Page_Load);
    this.classpicsDG.CancelCommand +=new DataGridCommandEventHandler(classpicsDG_CancelCommand);
    this.classpicsDG.UpdateCommand +=new DataGridCommandEventHandler(classpicsDG_UpdateCommand);
    this.classpicsDG.EditCommand +=new DataGridCommandEventHandler(classpicsDG_EditCommand);
    this.classpicsDG.ItemCreated +=new DataGridItemEventHandler(classpicsDG_ItemCreated);
    }
    private void classpicsDG_CancelCommand(object source, DataGridCommandEventArgs e)
    {
    this.classpicsDG.EditItemIndex = -1; UpdateView();
    } private void classpicsDG_EditCommand(object source, DataGridCommandEventArgs e)
    {
    this.classpicsDG.EditItemIndex = e.Item.ItemIndex; UpdateView();
    } private void classpicsDG_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
    const string UPDATE = " Update LotteryDetail Set FirstSelect = ?,FirstConfidence = ? , SecondSelect = ?, SecondConfidence = ? , LastSelect = ?, LastConfidence = ? Where LotteryID = ?";

    DbRoot db = DbFactory.GetDb();
    DbSqlText sql = new DbSqlText(UPDATE); for(int i = 3 ; i < 9 ; i++)
    {
    TextBox text = (TextBox)e.Item.Cells[i].Controls[0];
    sql.SetValue(text.Text.Replace("&nbsp;",""));
    }
    string key = classpicsDG.DataKeys[e.Item.ItemIndex].ToString();  sql.SetValue(7,key);
    db.Exec(sql.SqlCmd ); UpdateView(); } private void UpdateView()
    {
    DataSet ds = DbFactory.GetDb().FillDataSet("SELECT * FROM LotteryDetail "); if(ds!= null && ds.Tables.Count > 0)
    {
    classpicsDG.DataSource = ds.Tables[0];
    }
    classpicsDG.DataBind();

    } private void classpicsDG_ItemCreated(object sender, DataGridItemEventArgs e)
    {
    ListItemType li=e.Item.ItemType;
    if(li==ListItemType.EditItem)
    {
    for(int i= 3; i <9 ; i++)
    {

    TextBox txtNum=(TextBox)e.Item.Cells[i].Controls[0];//第三列的文本框
    txtNum.Width=Unit.Pixel(40);   //在这里设置宽度

    }
    }
    }/// .cs 中的东西有些是我自己的底层库,不要照搬,改成你自己的