欢迎光临!



首先,HTML代码:
<asp:GridView ID="SubjectGrid" runat="server">
    
</asp:GridView>
第二步,初始化GridView Columns:
void Page_Load(Object sender, EventArgs e)
{
    ShowGrid();
}

private void ShowGrid()
        {
            DataTable customerTable 
= new DataTable("Customers");
            
            grdSubject.AutoGenerateColumns 
= false;
            grdSubject.ShowHeader 
= false;
            grdSubject.DataKeyNames 
= new String[] { "Id" };
            DataControlFieldCollection dcfc 
= grdSubject.Columns;
            dcfc.Clear();
            BoundField bf;

            bf 
= new BoundField();
            bf.DataField 
= "Id";
            bf.Visible 
= false;
            dcfc.Add(bf);

            bf 
= new BoundField();
            bf.DataField 
= "Title";
            dcfc.Add(bf);

            bf 
= new BoundField();
            bf.DataField 
= "BeginDate";
            bf.SortExpression 
= "BeginDate";
            bf.HtmlEncode 
= false;
            bf.DataFormatString 
= "{0:yyyy-MM-dd}";
            dcfc.Add(bf);

            bf 
= new BoundField();
            bf.DataField 
= "EndDate";
            bf.HtmlEncode 
= false;
            bf.DataFormatString 
= "{0:yyyy-MM-dd}";
            dcfc.Add(bf);

            ButtonField selectRow 
= new ButtonField();
            selectRow.ButtonType 
= ButtonType.Button;
            selectRow.CommandName 
= "Select";
            selectRow.Text 
= "选择";
            dcfc.Add(selectRow);

            ButtonField delRow 
= new ButtonField();
            delRow.ButtonType 
= ButtonType.Button;
            delRow.AccessibleHeaderText 
= "Delete";
            delRow.CommandName 
= "Delete";
            delRow.Text 
= "删除";
            delRow.CausesValidation 
= true;
            dcfc.Add(delRow);

            grdSubject.DataSource 
= dt.DefaultView;
            grdSubject.DataBind();
        }
第三步:RowDataBound
void grdSubject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
//判断是否是DataRow
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                
//鼠标经过Row时的效果
                e.Row.Attributes.Add("onmouseover""e=this.style.backgroundColor; this.style.backgroundColor='linen'");
                e.Row.Attributes.Add(
"onmouseout""this.style.backgroundColor=e");

                
//当开始时间大于现在时间,显示行为蓝色
                if (DateTime.Parse(e.Row.Cells[2].Text) > DateTime.Now)
                {
                    e.Row.BackColor 
= Color.LightSkyBlue;
                }
                
//当结束时间小于现在时间,显示行为灰色
                if (DateTime.Parse(e.Row.Cells[3].Text) < DateTime.Now)
                {
                    e.Row.BackColor 
= Color.Silver;
                }

                
//当点击删除按钮时激活提示
                Button btn = (Button)e.Row.Cells[5].Controls[0];
                btn.Attributes.Add(
"onclick""javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
            }
        }
其中的关键是Button btn=(Button)e.Row.Cells[5].Controls[0]; 即声明此事件是由第6列中的第一个控件调用。

第四步:执行

void grdSubject_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        
void grdSubject_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
//单击Grid中按钮时发生throw new Exception("The method or operation is not implemented.");
            int selIndex = Convert.ToInt32(e.CommandArgument);
            GridViewRow selectedRow 
= grdSubject.Rows[selIndex];

            
if (e.CommandName == "Select")
            {
                txtSubjectTitle.Text 
= selectedRow.Cells[1].Text;
                dateBegin.Value 
= selectedRow.Cells[2].Text;
                dateEnd.Value 
= selectedRow.Cells[3].Text;

            }
            
if (e.CommandName == "Delete")
            {
                Hsf.Touch.Dto.Subject item 
= new Hsf.Touch.Dto.Subject();
                item.Id 
= int.Parse(grdSubject.DataKeys[selIndex].Value.ToString());
                
try
                {
                    TouchFactory.CreateSubjectManage().Delete(item);
                    
//删除成功,清除输入框内容
                    Clear();
                }
                
catch (Exception err)
                {
                    ShowMessageBox(
"删除失败 \n" + err.Message);
                }
            }
            
            ShowGrid();
        }
posted on 2008-06-05 17:19  花拉子米  阅读(1309)  评论(0编辑  收藏  举报
版权所有