GridView用法

来源:互联网 发布:淘宝一元云购 编辑:程序博客网 时间:2024/04/28 16:38

GridView常规用法

  1、gridview前台界面代码

  gridview创建列最主要的有两种方式:

  1)数据绑定,表示数据绑定控件中作为文本显示的字段。DataField ="AnswerNum",AnswerNum是数据源中的一个字段。举例说明: 

<asp:BoundField DataField ="AnswerNum" >        <ItemStyle Width ="8%" HorizontalAlign ="Center" /> </asp:BoundField>

  2)使用模板创建,举例说明: 

复制代码
<asp:TemplateField HeaderText ="查看">     <ItemTemplate >          <asp:LinkButton ID ="LinkButtonViewSOption" runat ="server" CommandName ="ViewSOption" CommandArgument ='<%# Bind("QO_ID") %>'>描</asp:LinkButton>     </ItemTemplate>     <ItemStyle Width ="5%" HorizontalAlign ="Center" /></asp:TemplateField>
复制代码

   ItemStyle是其模板样式,根据具体要求做出调整。

  

  2、绑定数据源 

this.gvQuestions.DataSource = ExamQuestionInfoList;                this.gvQuestions.DataBind();                this.gvQuestions.PageIndex = 0;

  gvQuestions为GridView控件,ExamQuestionInfoList为数据源,gridview的数据源可以是DataTable或者是数据集DataSet。

 

  3、停留在某一行变色

复制代码
private void ChangeColor(object sender, GridViewRowEventArgs e)        {            if (e.Row.RowType == DataControlRowType.DataRow)            {                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E6F5FA'");                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");            }        }

protected void gvQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
{
ChangeColor(sender, e);
}

复制代码

 

  4、操作某一行

  直接举例说明

复制代码
protected void gvSubjectiveOption_RowCommand(object sender, GridViewCommandEventArgs e)        {            int rowSelected = Convert.ToInt32(e.CommandArgument);            questionOptionInfo = QuestionOptionBLL.GetModel(rowSelected);            //查看            if (e.CommandName == "ViewSOption")            {                this.tbOptionStem.Text = questionOptionInfo.QO_Option;                this.tbCorrectAnswer.Text = questionOptionInfo.QO_SubjectAnswer;//主观题答案                this.tbCorrectAnswerExplain.Text = questionOptionInfo.QO_Explain;                //选项附件                string optionAccessoryStr = questionOptionInfo.QO_Accessory;                string[] optionAccessoryArr = optionAccessoryStr.Split(',');                for (int i = 0; i < optionAccessoryArr.Length; i++)                {                    OptionAccessoryList.Add(optionAccessoryArr[i]);                }                BindOptionAccessoryList();            }            if (e.CommandName == "DeleteOption")            {                QuestionOptionBLL.Delete(rowSelected);                int EQ_ID = questionOptionInfo.EQ_ID;                BindSubjectiveOption(EQ_ID);//重新绑定主观题问题信息            }        }
复制代码

  e.CommandName对应前台界面的一些字段: 

复制代码
<asp:TemplateField HeaderText ="查看">        <ItemTemplate >             <asp:LinkButton ID ="LinkButtonViewSOption" runat ="server" CommandName ="ViewSOption" CommandArgument ='<%# Bind("QO_ID") %>'>描</asp:LinkButton>        </ItemTemplate>        <ItemStyle Width ="5%" HorizontalAlign ="Center" /></asp:TemplateField><asp:TemplateField HeaderText ="删除" >       <ItemTemplate >            <asp:ImageButton ID ="ImageButtonDelete2" runat ="server"  BorderStyle ="None" CommandName ="DeleteOption" CommandArgument ='<%# Bind("QO_ID") %>' ImageUrl ="~/images/delete.gif" />        </ItemTemplate>      <ItemStyle Width ="5%" HorizontalAlign ="Center" />
</asp:TemplateField>
复制代码

  其中CommandName ="DeleteOption" CommandArgument ='<%# Bind("QO_ID") %>代表数据集中的某个字段。

  

  5、添加Checkbox并且初始化台界面:

复制代码
<asp:TemplateField >     <ItemTemplate >         <asp:LinkButton ID ="LinkButton1" runat ="server" CommandName ="selectCorrectAnswer" CommandArgument ='<%# Bind("QO_ID") %>'>           <asp:CheckBox ID ="cbCorrectAnswer" runat ="server" />         </asp:LinkButton>
</ItemTemplate>
复制代码

   后台逻辑: 

复制代码
/// <summary>        /// 初始化checkbox值        /// </summary>        /// <param name="gv">gridview控件</param>        /// <param name="dtSource">数据源</param>        /// <param name="cbName">checkbox控件名称</param>        /// <param name="cbValue">checkbox的值</param>        private void InitializeCheckBox(GridView gv, DataTable dtSource, string cbName, string cbValue)        {            int count = dtSource.Rows.Count;            if (count > 0)            {                for (int i = 0; i < count; i++)                {                    CheckBox cb = gv.Rows[i].FindControl(cbName) as CheckBox;                    if (cb != null)                    {                        if (dtSource.Rows[i][cbValue].ToString() == "0")                        {                            cb.Checked = false;                        }                        else                        {                            cb.Checked = true;                        }                    }                }            }        }
复制代码

    

  6、去掉gridview自带的分页数字

  因为项目中在使用gridview时需要用到分页,而它本身的分页显得不足以表达项目所以表现的操作,所以需要添加新的分页,必然需要去到它原来的分页。

  1)首先如果分页,必然要把属性AllowPaging设置为true。

  2)PagerSettings-Visible属性设置为false,分页数字自此去掉。

  3)手动添加分页,已经写出来了,但是项目还没有测试到,所以等此功能测试完毕后再添加此部分。

  

0 0