您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關GridView怎么在asp.net中使用,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
前臺代碼:
<asp:GridView ID="tabgv" runat="server" DataKeyNames="ysId" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated" AutoGenerateColumns="False" Width="99%" CssClass="gridview" EnableModelValidation="True" OnRowCancelingEdit="tabgv_RowCancelingEdit" OnRowEditing="tabgv_RowEditing" OnRowUpdating="tabgv_RowUpdating"> <RowStyle BorderColor="Black" BorderStyle="None" /> <Columns> <asp:BoundField DataField="gsHkrno" HeaderText="項目編號" ReadOnly="True"> <ItemStyle Width="5%" HorizontalAlign="Center" /> <FooterStyle BorderWidth="0px" /> </asp:BoundField> <asp:TemplateField HeaderText="項目名稱"> <ItemTemplate> <asp:HyperLink ID="Hyp_gsName" runat="server" ForeColor="Blue" Text='<%# Bind("gsName") %>'></asp:HyperLink> </ItemTemplate> <ItemStyle Width="25%" HorizontalAlign="left" /> <FooterTemplate> <asp:Label ID="Label4" runat="server" Font-Bold="True" Text="合 計:" Width="61px"></asp:Label> </FooterTemplate> <FooterStyle BorderColor="Black" BorderWidth="0px" HorizontalAlign="Right" /> </asp:TemplateField> <asp:TemplateField HeaderText="概算金額"> <ItemTemplate> <asp:Label ID="labgsMoney" runat="server" Text='<%#Bind("gsMoney", "{0:N2}") %>'></asp:Label> </ItemTemplate> <ItemStyle Width="8%" HorizontalAlign="right" /> <FooterTemplate> <asp:Label ID="labtotalgsMoney" runat="server" ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyle BorderColor="Black" HorizontalAlign="Right" /> </asp:TemplateField> <asp:TemplateField HeaderText="開工至上年底\n累計完成概算"> <ItemTemplate> <asp:Label ID="labkgzsndwcGsMoney" runat="server" Text='<%# Bind("footMoney", "{0:N2}") %>'></asp:Label> <asp:HiddenField ID="hdnysYear" runat="server" Value='<%# Eval("ysYear") %> ' /> <asp:HiddenField ID="hdngsId" runat="server" Value='<%# Eval("gsId") %> ' /> <asp:HiddenField ID="hdngsHkrno" runat="server" Value='<%# Eval("gsHkrno") %> ' /> <asp:HiddenField ID="hdnChildFlag" runat="server" Value='<%# Eval("childFlag") %> ' /> </ItemTemplate> <ItemStyle Width="8%" HorizontalAlign="right" /> <FooterTemplate> <asp:Label ID="labtotalfootMoney" runat="server" ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyle BorderColor="Black" HorizontalAlign="Right" /> </asp:TemplateField> <asp:TemplateField HeaderText="投資計劃金額"> <ItemTemplate> <asp:Label ID="labtzqysMoney" runat="server" Text='<%# Bind("tzqysMoney", "{0:N2}") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txttzqysMoney" runat="server" Text='<%# Eval("tzqysMoney") %> ' Style="text-align: right" Width="100px" onKeyPress="if (event.keyCode!=46 && event.keyCode!=45 && (event.keyCode<48 || event.keyCode>57)) event.returnValue=false"></asp:TextBox> </EditItemTemplate> <ItemStyle Width="8%" HorizontalAlign="right" /> <FooterTemplate> <asp:Label ID="labtotaltzqysMoney" runat="server" ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyle BorderColor="Black" HorizontalAlign="Right" /> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="labtzhysMoney" runat="server" Text='<%# Bind("tzhysMoney", "{0:N2}") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txttzhysMoney" runat="server" Text='<%# Eval("tzhysMoney") %> ' Style="text-align: right" Width="100px" onKeyPress="if (event.keyCode!=46 && event.keyCode!=45 && (event.keyCode<48 || event.keyCode>57)) event.returnValue=false"></asp:TextBox> </EditItemTemplate> <ItemStyle Width="8%" HorizontalAlign="right" /> <FooterTemplate> <asp:Label ID="labtotaltzhysMoney" runat="server" ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyle BorderColor="Black" HorizontalAlign="Right" /> </asp:TemplateField> <asp:TemplateField HeaderText="備注"> <ItemTemplate> <asp:Label ID="labtremark" runat="server" Text='<%# Eval("remark") %> '></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtremark" runat="server" Text='<%# Eval("remark") %> ' Width="100px"></asp:TextBox> </EditItemTemplate> <ItemStyle Width="10%" HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:LinkButton ID="LkBtnUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton> <asp:LinkButton ID="LkBtnCtrl" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton> </EditItemTemplate> <ItemTemplate> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName='<%#Eval("childFlag").ToString()== "0" ? "":"Edit"%>' Text='<%#Eval("childFlag").ToString()== "0" ? "鎖定":"編輯"%>'></asp:LinkButton> </ItemTemplate> <ItemStyle Width="8%" HorizontalAlign="Center" /> </asp:TemplateField> </Columns> </asp:GridView>
注意:
1、通過設置 ReadOnly="True" 表示此列不可以編輯;
2、通過Text='<%# Bind("tzhysMoney", "{0:N2}") %>'進行金額格式化;
3、重點的編輯\顯示\合計
<asp:TemplateField> <ItemTemplate> <asp:Label ID="labtzhysMoney" runat="server" Text='<%# Bind("tzhysMoney", "{0:N2}") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txttzhysMoney" runat="server" Text='<%# Eval("tzhysMoney") %> ' Style="text-align: right" Width="100px" ></asp:TextBox> </EditItemTemplate> <ItemStyle Width="8%" HorizontalAlign="right" /> <FooterTemplate> <asp:Label ID="labtotaltzhysMoney" runat="server" ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyle BorderColor="Black" HorizontalAlign="Right" /> </asp:TemplateField>
后臺代碼:
行綁定時的操作:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow gr = e.Row; if (gr.RowType == DataControlRowType.DataRow) { HiddenField hdnysYear = (HiddenField)e.Row.FindControl("hdnysYear"); HiddenField hdngsHkrno = (HiddenField)e.Row.FindControl("hdngsHkrno"); HiddenField hdnChildFlag = (HiddenField)e.Row.FindControl("hdnChildFlag"); HyperLink hy = (HyperLink)e.Row.FindControl("Hyp_gsName"); if (hdnChildFlag.Value!="1") { //設置連接地址\參數 hy.NavigateUrl = "ys_edit_right.aspx?gsHkrno=" + hdngsHkrno.Value + "&sysObjNo=" + sysObjNo + "&ysYear=" + hdnysYear.Value; hy.Font.Underline = true; hy.ForeColor = System.Drawing.Color.Blue; hy.Target = "mainFrame"; } else { hy.ForeColor = System.Drawing.Color.Black; } //計算合計 Label labgsMoney = (Label)(e.Row.FindControl("labgsMoney")); if (labgsMoney != null || labgsMoney.Text != "") { gsMoneytotal += Convert.ToDecimal(labgsMoney.Text); } Label labkgzsndwcGsMoney = (Label)(e.Row.FindControl("labkgzsndwcGsMoney")); if (labkgzsndwcGsMoney != null || labkgzsndwcGsMoney.Text != "") { footMoneytotal += Convert.ToDecimal(labkgzsndwcGsMoney.Text); } //編輯狀態時,不然點擊編輯時會出錯,找不到控件 if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit) { TextBox txttzqysMoney = (TextBox)(e.Row.FindControl("txttzqysMoney")); if (txttzqysMoney != null || txttzqysMoney.Text != "") { tzqysMoneytotal += Convert.ToDecimal(txttzqysMoney.Text); } TextBox txttzhysMoney = (TextBox)(e.Row.FindControl("txttzhysMoney")); if (txttzhysMoney != null || txttzhysMoney.Text != "") { tzhysMoneytotal += Convert.ToDecimal(txttzhysMoney.Text); } } else //非編輯狀態時 { Label labtzqysMoney = (Label)(e.Row.FindControl("labtzqysMoney")); if (labtzqysMoney != null || labtzqysMoney.Text != "") { tzqysMoneytotal += Convert.ToDecimal(labtzqysMoney.Text); } Label labtzhysMoney = (Label)(e.Row.FindControl("labtzhysMoney")); if (labtzhysMoney != null || labtzhysMoney.Text != "") { tzhysMoneytotal += Convert.ToDecimal(labtzhysMoney.Text); } } } } //取消編輯 protected void tabgv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { tabgv.EditIndex = -1; string strysYear = (Convert.ToInt32(ysYear) - 1).ToString(); GvDataBind(gsHkrno, "0002", ysYear, "1900-01-01 00:00:00", strysYear + "-12-25 00:00:00"); } //編輯 protected void tabgv_RowEditing(object sender, GridViewEditEventArgs e) { tabgv.EditIndex = e.NewEditIndex; string strysYear = (Convert.ToInt32(ysYear) - 1).ToString(); GvDataBind(gsHkrno, "0002", ysYear, "1900-01-01 00:00:00", strysYear + "-12-25 00:00:00"); } //更新 protected void tabgv_RowUpdating(object sender, GridViewUpdateEventArgs e) { string sqlstr = "update ys_ysitem_list set tzqysMoney=@tzqysMoney,tzhysMoney=@tzhysMoney,remark=@remark where ysId=@ysId"; SqlParameter[] para ={new SqlParameter("@tzqysMoney",SqlDbType.VarChar), new SqlParameter("@tzhysMoney",SqlDbType.VarChar), new SqlParameter("@remark",SqlDbType.VarChar), new SqlParameter("@ysId",SqlDbType.VarChar) }; para[0].Value = ((TextBox)(tabgv.Rows[e.RowIndex].FindControl("txttzqysMoney"))).Text.ToString().Trim(); para[1].Value = ((TextBox)(tabgv.Rows[e.RowIndex].FindControl("txttzhysMoney"))).Text.ToString().Trim(); para[2].Value = ((TextBox)(tabgv.Rows[e.RowIndex].FindControl("txtremark"))).Text.ToString().Trim(); para[3].Value = tabgv.DataKeys[e.RowIndex].Value.ToString(); pms.SqlHelper.ExecuteNonQuery(CommandType.Text, sqlstr, para); tabgv.EditIndex = -1; string strysYear = (Convert.ToInt32(ysYear) - 1).ToString(); GvDataBind(gsHkrno, "0002", ysYear, "1900-01-01 00:00:00", strysYear + "-12-25 00:00:00"); }
上述就是小編為大家分享的GridView怎么在asp.net中使用了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。