本文介绍了将列表绑定到GridView和Upadte GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好朋友, 首先祝大家情人节快乐。 现在回答我的问题 在我的页面我使用List绑定gridview。 i在我的页面上有不同的字段并将这些字段值存储到列表中并将其与网格绑定 它运行良好但现在我想要使用gridview中的复选框更新gridview,点击复选框,该行中的关联值出现在不同的字段上,按钮保存点击gridview应该更新。 i不知道如何实现它。 请帮助我解决这个问题。 先谢谢 我的HTML代码是这样的/> < table class = tblPopUp > < tr > < td > < asp:标签 ID = lblCaste CssClass = 标签 runat = server 文字 = Caste > < / asp:标签 > < / td > < td > < asp:DropDownList ID = ddlCaste runat = server CssClass = 下拉列表myddlinPopUp > < / asp:DropDownList > < / td > < / tr > < tr > < td > < asp:Label ID = lblNo_OfPosition CssClass = 标签 runat = 服务器 正文 = No Of Position > < / asp:Label > < / td > < td > < asp:TextBox ID = txtNoOf PositioninPopUp runat = server CssClass = TextBox mytextboxinPopUp > < / asp:TextBox > < / td > < / tr > < tr > < td > < asp:标签 ID = lblMinAge CssClass = 标签 runat = 服务器 文本 = 最小年龄 > < / asp:Label > < / td > < td > < asp:TextBox ID = txtMinAge runat = server CssClass = TextBox mytextboxinPopUp > < / asp:TextBox > < / td > < / tr > < tr > < td > < asp:Label ID = lblMaxAge CssClass = 标签 runat = server 文字 = 最大年龄 > < / asp:Label > < / td > < td > < asp:TextBox ID = txtMaxAge runat = server CssClas s = TextBox mytextboxinPopUp > < / asp:TextBox > < / td > < / tr > < tr > < td > < asp:按钮 ID = btnSaveInPopUpReservation runat = 服务器 CssClass = 按钮 文字 = 保存 OnClick = btnSaveInPopUpReservation_Click / > < / td > ; < / tr > < / table > < asp:GridView ID = gridReservationDetails runat = server GridLines = 两者 AutoGenerateColumns = false CssClass = gridview ShowHeaderWhenEmpty = true > < 列 > < asp:TemplateField HeaderStyle-Horizo​​ntalAlign = 左 > < HeaderTemplate > < asp:CheckBox ID = checkAll runat = server onclick = checkAll(this); / > < / HeaderTemplate > < ItemTemplate > < asp:CheckBox ID = CheckBox1 runat = server onclick = Check_Click(this) / > < / ItemTemplate > < HeaderStyle Font-Bold = False / > < ItemStyle Font-Bold = 错误 / > < / asp:TemplateField > < asp:TemplateField HeaderText = Sl。否 HeaderStyle-Horizo​​ntalAlign = 左 ItemStyle-CssClass = item > < ItemTemplate > <% #Container.DataItemIndex + 1 %> < / ItemTemplate > < / asp:TemplateField > < asp: BoundField HeaderText = Caste DataField = casteName ItemStyle-CssClass = item HeaderStyle-Horizo​​ntalAlign = 左 / > < asp:BoundField HeaderText = No Of Position DataField = NoOfPosition ItemStyle-CssClass = item HeaderStyle-Horizo​​ntalAlign = 左 / > < asp:BoundField HeaderText = Min Age 数据字段 = MinAge ItemStyle-CssClass = item HeaderStyle-Horizo​​ntalAlign = 左 / > < asp:BoundField HeaderText = Max Age DataField = MaxAge ItemStyle-CssClass = item HeaderStyle-Horizo​​ntalAlign = Left / > ; < / Columns > < / asp:GridView > 绑定列表值后面的代码就像这样 public class CommonHRMS_PositionRequisitionReservationDetails { public int casteID { get ; set ; } public string casteName { get ; set ; } public int NoOfPosition { get ; set ; } public int MaxAge { get ; set ; } public int MinAge { get ; set ; } } protected 列表< CommonHRMS_PositionRequisitionReservationDetails> FillReservation() { List< CommonHRMS_PositionRequisitionReservationDetails> lst = new 列表< CommonHRMS_PositionRequisitionReservationDetails>(); 列表< CommonHRMS_PositionRequisitionReservationDetails> lstPrevious = new 列表< CommonHRMS_PositionRequisitionReservationDetails>(); lstPrevious =(List< CommonHRMS_PositionRequisitionReservationDetails>)会话[ cprrd]; if (lstPrevious!= null ) { CommonHRMS_PositionRequisitionReservationDetails obj = new CommonHRMS_PositionRequisitionReservationDetails(); obj.casteID = int .Parse(ddlCaste.SelectedValue); obj.casteName = ddlCaste.SelectedItem.ToString(); obj.NoOfPosition = int .Parse(txtNoOfPositioninPopUp.Text); obj.MaxAge = int .Parse(txtMaxAge.Text); obj.MinAge = int .Parse(txtMinAge.Text); lstPrevious.Add(obj); 会话[ cprrd] = lstPrevious; return lstPrevious; } else { CommonHRMS_PositionRequisitionReservationDetails obj = new CommonHRMS_PositionRequisitionReservationDetails(); obj.casteID = int .Parse(ddlCaste.SelectedValue); obj.casteName = ddlCaste.SelectedItem.ToString(); obj.NoOfPosition = Convert.ToInt32(txtNoOfPositioninPopUp.Text); obj.MaxAge = Convert.ToInt32(txtMaxAge.Text); obj.MinAge = Convert.ToInt32(txtMinAge.Text); lst.Add(obj); 会话[ cprrd] = lst; return lst; } } protected void btnSaveInPopUpReservation_Click( object sender,EventArgs e) { List< CommonHRMS_PositionRequisitionReservationDetails> lst = FillReservation(); gridReservationDetails.DataSource = lst; gridReservationDetails.DataBind(); } 解决方案 好的,逻辑将会跟随。 放置更新按钮。 处理更新按钮单击事件。 在按钮,遍历所有 GridView 行。 在循环内找到 CheckBox 控件。 如果选中了 CheckBox ,请将更新语句附加到声明的 StringBuilder 。 循环之后,执行 StringBuilder ,您的更新将完成。 请参阅一个示例 - 使用复选框编辑更新删除多个记录/行Gridview [ ^ ] Hello Friends,First Of all Happy Valentine Day to All of You.now come to my questionin my page i am binding a gridview using List.i have different fields on my page and storing those fields values into list and binding it with a gridit is working well but now i want to update gridview using checkbox in gridview,on clicking on checkbox the associated values in that row comes on the different fields and on button save click gridview should be updated.i don't know how to achieve it.kindly help me fix this issue.Thanks In AdvanceMy HTML Code is like this<table class="tblPopUp"><tr><td><asp:Label ID="lblCaste" CssClass="Label" runat="server" Text="Caste"></asp:Label></td><td> <asp:DropDownList ID="ddlCaste" runat="server" CssClass="Dropdownlist myddlinPopUp"></asp:DropDownList></td></tr><tr><td><asp:Label ID="lblNo_OfPosition" CssClass="Label" runat="server" Text="No Of Position"></asp:Label></td><td><asp:TextBox ID="txtNoOfPositioninPopUp" runat="server" CssClass="TextBox mytextboxinPopUp"></asp:TextBox></td></tr><tr><td><asp:Label ID="lblMinAge" CssClass="Label" runat="server" Text="Min Age"></asp:Label> </td> <td> <asp:TextBox ID="txtMinAge" runat="server" CssClass="TextBox mytextboxinPopUp"></asp:TextBox></td></tr><tr><td><asp:Label ID="lblMaxAge" CssClass="Label" runat="server" Text="Max Age"></asp:Label></td><td><asp:TextBox ID="txtMaxAge" runat="server" CssClass="TextBox mytextboxinPopUp"></asp:TextBox> </td></tr><tr><td><asp:Button ID="btnSaveInPopUpReservation" runat="server" CssClass="button" Text="Save" OnClick="btnSaveInPopUpReservation_Click" /> </td></tr></table><asp:GridView ID="gridReservationDetails" runat="server" GridLines="Both" AutoGenerateColumns="false" CssClass="gridview" ShowHeaderWhenEmpty="true"><Columns><asp:TemplateField HeaderStyle-HorizontalAlign="Left"><HeaderTemplate><asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" /> </HeaderTemplate><ItemTemplate><asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" /> </ItemTemplate> <HeaderStyle Font-Bold="False" /> <ItemStyle Font-Bold="False" /></asp:TemplateField><asp:TemplateField HeaderText="Sl. No" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="item"><ItemTemplate> <%# Container.DataItemIndex + 1 %> </ItemTemplate></asp:TemplateField> <asp:BoundField HeaderText="Caste" DataField="casteName" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" /><asp:BoundField HeaderText="No Of Position" DataField="NoOfPosition" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" /><asp:BoundField HeaderText="Min Age" DataField="MinAge" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" /><asp:BoundField HeaderText="Max Age" DataField="MaxAge" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" /></Columns></asp:GridView>my code behind for binding list values is like thispublic class CommonHRMS_PositionRequisitionReservationDetails { public int casteID { get; set; } public string casteName { get; set; } public int NoOfPosition { get; set; } public int MaxAge { get; set; } public int MinAge { get; set; } }protected List<CommonHRMS_PositionRequisitionReservationDetails> FillReservation() { List<CommonHRMS_PositionRequisitionReservationDetails> lst = new List<CommonHRMS_PositionRequisitionReservationDetails>(); List<CommonHRMS_PositionRequisitionReservationDetails> lstPrevious = new List<CommonHRMS_PositionRequisitionReservationDetails>(); lstPrevious = (List<CommonHRMS_PositionRequisitionReservationDetails>)Session["cprrd"]; if (lstPrevious != null) { CommonHRMS_PositionRequisitionReservationDetails obj = new CommonHRMS_PositionRequisitionReservationDetails(); obj.casteID = int.Parse(ddlCaste.SelectedValue); obj.casteName = ddlCaste.SelectedItem.ToString(); obj.NoOfPosition = int.Parse(txtNoOfPositioninPopUp.Text); obj.MaxAge = int.Parse(txtMaxAge.Text); obj.MinAge = int.Parse(txtMinAge.Text); lstPrevious.Add(obj); Session["cprrd"] = lstPrevious; return lstPrevious; } else { CommonHRMS_PositionRequisitionReservationDetails obj = new CommonHRMS_PositionRequisitionReservationDetails(); obj.casteID = int.Parse(ddlCaste.SelectedValue); obj.casteName = ddlCaste.SelectedItem.ToString(); obj.NoOfPosition = Convert.ToInt32(txtNoOfPositioninPopUp.Text); obj.MaxAge = Convert.ToInt32(txtMaxAge.Text); obj.MinAge = Convert.ToInt32(txtMinAge.Text); lst.Add(obj); Session["cprrd"] = lst; return lst; } }protected void btnSaveInPopUpReservation_Click(object sender, EventArgs e) { List<CommonHRMS_PositionRequisitionReservationDetails> lst = FillReservation(); gridReservationDetails.DataSource = lst; gridReservationDetails.DataBind(); } 解决方案 Okay, logic would be following.Place a Update Button.Handle Update Button Click Event.Inside that Button, Loop through all the GridView Rows.Inside the Loop find the CheckBox Control.If that CheckBox is checked, append Update Statement to declared StringBuilder.After the Loop, Execute the that StringBuilder and your update will be done.See one Example - Edit Update Delete Multiple Records/Rows Gridview With Checkbox[^]. 这篇关于将列表绑定到GridView和Upadte GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 21:17