问题描述
GridView控件的使用方法
解决方案
解决方案二:
列的分类a:BoundField:适合绑定数据表的源列b:TempleteField:模板列:具体组成(1):HeadTemplete头模板(2):FooterTemplete尾模板(3):ItemTemplete项模板绑定表达式(1):分类:前期绑定(静态绑定)和后期绑定(动态绑定)<%#Eval("字段名或者属性名")%>只读绑定方法<%#Bind("同上"%>可读写的绑定方法1.把GridView从工具箱拖入页面.2.在"设计"页面,点GridView会看到‘[>]’,然后,点击编辑列TemplateField或BoundField在源页面会自动生成<asp:BoundField/>,<asp:TemplateField></asp:TemplateField>,BoundField是用DataField="列"绑定数据,而TemplateField是在<ItemTemplate><%#Eval("列")%></ItemTemplate>绑定数据3.连接数据库GridView1.DataSource=(连接数据库--DataTable),GridView1.DataBind();
解决方案三:
引用1楼hh520xb的回复:
列的分类a:BoundField:适合绑定数据表的源列b:TempleteField:模板列:具体组成(1):HeadTemplete头模板(2):FooterTemplete尾模板(3):ItemTemplete项模板绑定表达式(1):分类:前期绑定(静态绑定)和后期绑定(动态绑定)<%#Eval("字段名或者属性名")%>只读绑定方法<%#Bind("同上"%>可读写的绑定方法1.把GridView从工具箱拖入页面.2.在"设计"页面,点GridVie…
你去网上搜索一下清清月儿写的gridView72绝技非常好全面
解决方案四:
这个东西在网上很多,你具体要什么?说出需求,可能回答的针对性强一些
解决方案五:
它的功能太多,这怎能说得完。一般也不用它。因为效率太低
解决方案六:
忍,baidu搜一下一大堆...
解决方案七:
1.GridView中使用超链接的技巧GridView中的超级链接,可以设置一个模版列,放入超级链接的控件,设置绑定参数即可。数据绑定方式有两种,如下示例:◆Eval方式<%#Eval("id")%>◆Bind方式<%#Bind("id","~/info.aspx?id={0}")%>推荐使用第一种方式,可以在一个<%#%>里放入多个绑定,而第二种只能如此绑定一个值<%#Eval("id")+":"+Eval("name")%>做超级链接的控件,我们也有多种选择:◆asp:LinkButton示例<asp:LinkButtonID="LinkButton2"OnClientClick=<%#"open('info.aspx?id="+Eval("id")+"&name="+Eval("name")+"')"%>runat="server"><%#"LinkButton方式绑定:"+Eval("id")%></asp:LinkButton>◆asp:HyperLink示例<asp:HyperLinkID="HyperLink1"runat="server"NavigateUrl='<%#"~/info.aspx?id="+Eval("id")+"&name="+Eval("name")%>'Text='<%#"HyperLink控件:"+Eval("id")%>'></asp:HyperLink>◆a标签示例<ahref='<%#"info.aspx?id="+Eval("id")+"&name="+Eval("name")%>'>a标签:<%#Eval("id")+":"+Eval("name")%></a><ahref='Java:alert("<%#"~/info.aspx?id="+Eval("id")+"&name="+Eval("name")%>")'>链接js事件:<%#Eval("id")%></a>LinkButton不好设置,推荐使用HyperLink或者a的方式,简单实用。特别是a标签,使用Java的方法也比较方便。补充:<asp:LinkButtonID="LinkButton3"runat="server"PostBackUrl='<%#"~/Default.aspx?id="+Eval("FUserID")%>'CausesValidation="False"Text="跳转编辑"></asp:LinkButton>2.gridview模板列加htmlinputcheckbox的取值①aspx文件:<asp:GridViewID="GridView1"runat="server"AllowPaging="True"AutoGenerateColumns="False"DataKeyNames="OrderID,ProductID"DataSourceID="SqlDataSource1"EmptyDataText="111"><Columns><asp:BoundFieldDataField="OrderID"HeaderText="OrderID"ReadOnly="True"SortExpression="OrderID"/><asp:BoundFieldDataField="ProductID"HeaderText="ProductID"ReadOnly="True"SortExpression="ProductID"/><asp:BoundFieldDataField="UnitPrice"HeaderText="UnitPrice"SortExpression="UnitPrice"DataFormatString="{0:C}"/><asp:BoundFieldDataField="Quantity"HeaderText="Quantity"SortExpression="Quantity"/><asp:BoundFieldDataField="Discount"HeaderText="Discount"SortExpression="Discount"/><asp:ImageFieldDataImageUrlField="OrderID"DataImageUrlFormatString="/{0}.gif"HeaderText="pic"></asp:ImageField><asp:TemplateFieldFooterText="id"><ItemTemplate> <inputid="Checkbox1"runat="server"type="checkbox"='<%#Eval("OrderID")%>'/></ItemTemplate></asp:TemplateField></Columns></asp:GridView><%=gridview1.PageIndex+1%>of<%=GridView1.PageCount%>②cs文件:stringstr1;HtmlInputCheckBoxcb;str1="";for(inti=0;i<GridView1.Rows.Count-1;i++){cb=GridView1.Rows(i).Cells(6).FindControl("Checkbox1");if(cb.Checked){if(str1==""){str1=cb.;}else{str1=str1+","+cb.;}}}Label1.Text=str1;
解决方案八:
3.gridview模板列绑定gridview①aspx文件:<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="ProductID"DataSourceID="SqlDataSource1"><Columns><asp:BoundFieldDataField="ProductID"HeaderText="ProductID"InsertVisible="False"ReadOnly="True"SortExpression="ProductID"/><asp:BoundFieldDataField="ProductName"HeaderText="ProductName"SortExpression="ProductName"/><asp:TemplateFieldHeaderText="detail"><ItemTemplate> <asp:GridViewID="GridView2"runat="server"></asp:GridView></ItemTemplate></asp:TemplateField></Columns></asp:GridView>②cs文件:protectedvoidGridView1_RowDataBound(objectsender,System.Web.UI.WebControls.GridViewRowEventArgse){if(e.Row.RowType==DataControlRowType.DataRow){SqlDataSource2.SelectParameters.Clear();SqlDataSource2.SelectParameters.Add("id",e.Row.Cells(0).Text);GridViewgv;gv=e.Row.Cells(2).FindControl("GridView2");gv.DataSource=this.SqlDataSource2;gv.DataBind();}}4.GridView更新和删除显示提示信息①aspx文件:<asp:GridViewID="GridView1"runat="server"DataSourceID="SqlDataSource1"AutoGenerateColumns="False"DataKeyNames="au_id"OnRowCommand="GridView1_RowCommand"OnRowUpdating="GridView1_RowUpdating"OnRowDeleting="GridView1_RowDeleting"><Columns><asp:TemplateField><ItemTemplate><asp:LinkButtonID="Edit"runat="server"CommandName="Edit">编辑</asp:LinkButton></ItemTemplate></asp:TemplateField><asp:TemplateFieldVisible="False"><ItemTemplate><asp:LinkButtonID="Update"runat="server"CommandName="Update"OnClientClick="returnconfirm('确定要更新?');">更新</asp:LinkButton> <asp:LinkButtonID="Delete"runat="server"CommandName="Delete"OnClientClick="returnconfirm('确定要删除');">删除</asp:LinkButton> <asp:LinkButtonID="Cancel"runat="server"CommandName="Cancel">取消</asp:LinkButton></ItemTemplate></asp:TemplateField><asp:templatefieldheadertext="LastName"><itemtemplate><%#Eval("au_lname")%></itemtemplate><edititemtemplate><asp:textboxid="LastNameTextBox"text='<%#Eval("au_lname")%>'width="175"runat="server"/><br/><asp:requiredfieldvalidatorid="LastNameRequiredValidator"controltovalidate="LastNameTextBox"errormessage="Pleaseenteralastname."validationgroup="NameGroup"runat="server"/></edititemtemplate></asp:templatefield><asp:templatefieldheadertext="FirstName"><itemtemplate><%#Eval("au_fname")%></itemtemplate><edititemtemplate><asp:textboxid="FirstNameTextBox"text='<%#Eval("au_fname")%>'width="175"runat="server"/><br/><asp:requiredfieldvalidatorid="FirstNameRequiredValidator"controltovalidate="FirstNameTextBox"errormessage="Pleaseenterafirstname."validationgroup="NameGroup"runat="server"/></edititemtemplate></asp:templatefield></Columns></asp:GridView><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:Pubs2000%>"ProviderName="<%$ConnectionStrings:Pubs2000.ProviderName%>"SelectCommand="SELECTau_id,au_lname,au_fnameFROMauthors"UpdateCommand="UPDATEauthorsSETau_lname=@au_lname,au_fname=@au_fnameWHERE(au_id=@au_id)"DeleteCommand="DELETEFROMauthorsWHERE(au_id=@au_id)"></asp:SqlDataSource>②cs文件:voidGridView1_RowCommand(objectsender,GridViewCommandEventArgse){switch(e.CommandName.ToLower()){case"edit":GridView1.Columns[0].Visible=false;GridView1.Columns[1].Visible=true;break;case"update":case"delete":case"cancel":GridView1.Columns[0].Visible=true;GridView1.Columns[1].Visible=false;break;default://Donothing.break;}}voidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse){intindex=GridView1.EditIndex;GridViewRowrow=GridView1.Rows[index];TextBoxfirstNameTextBox=(TextBox)row.Cells[1].FindControl("FirstNameTextBox");StringfirstName="";if(firstNameTextBox!=null){firstName=firstNameTextBox.Text;}TextBoxlastNameTextBox=(TextBox)row.Cells[2].FindControl("LastNameTextBox");StringlastName="";if(lastNameTextBox!=null){lastName=lastNameTextBox.Text;}ParameterlastNameParameter=newParameter("au_lname",TypeCode.String,lastName);ParameterfirstNameParameter=newParameter("au_fname",TypeCode.String,firstName);SqlDataSource1.UpdateParameters.Clear();SqlDataSource1.UpdateParameters.Add(lastNameParameter);SqlDataSource1.UpdateParameters.Add(firstNameParameter);}voidGridView1_RowDeleting(objectsender,GridViewDeleteEventArgse){SqlDataSource1.DeleteParameters.Clear();}
解决方案九:
baidu里搜"gridview72般绝技"