首页 新闻 会员 周边 捐助

GridView自带的编辑和更新,点击编辑正常,点更新时一直报索引超出范围

0
[待解决问题]
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Web.UI.WebControls;
  7 using System.Web.UI.HtmlControls;
  8 using System.Web.UI.WebControls.WebParts;
  9 using citd_BHwebmanage.Wechat;
 10 using citd_BHwebmanage.Data;
 11 using System.Data;
 12 using System.Data.SqlClient;
 13 
 14 namespace citd_BHwebmanage.views
 15 {
 16     public partial class wEarlyStregy1 : System.Web.UI.Page
 17     {
 18         protected void Page_Load(object sender, EventArgs e)
 19         {
 20             if(!IsPostBack)
 21             {
 22                 BindData();                
 23             }
 24             else
 25             {
 26 
 27             }
 28         }
 29 
 30         private void BindData()
 31         {
 32             List<Monitor_send_list> objmsl = new List<Monitor_send_list>();
 33             objmsl = MonitorsendlistOper.SelectMsl();
 34             this.GridView1.DataKeyNames = new string[] { "MonitorsendID" };
 35 
 36             this.GridView1.DataSource = objmsl;
 37             
 38             this.GridView1.DataBind();
 39         }
 40 
 41         protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 42         {
 43             if (e.Row.RowType == DataControlRowType.DataRow)
 44             {
 45                 if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
 46                 {
 47                     try
 48                     {
 49                         Label t = e.Row.FindControl("lbMonitorsendrauth") as Label;
 50                         string rst = "未知";
 51 
 52                         if ((e.Row.DataItem as Monitor_send_list).Monitorsendrauth == 0)
 53                             rst = "";
 54                         if ((e.Row.DataItem as Monitor_send_list).Monitorsendrauth == 1)
 55                             rst = "";
 56                        
 57                         t.Text = rst;
 58                     }
 59                     catch (Exception err)
 60                     {
 61                         ClientScript.RegisterStartupScript(typeof(Page), "err", "alert('加载读取列表错误:" + err.Message + "');", true);
 62                     }
 63                     //Label lbBridgename = e.Row.FindControl("lbBridgename") as Label;
 64                     //if(lbBridgename != null)
 65                     //{
 66                         
 67                     //}
 68                 }
 69                 if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
 70                 {
 71                 }
 72             }
 73             
 74            
 75         }
 76 
 77         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
 78         {
 79             GridView1.EditIndex = e.NewEditIndex;
 80             BindData();
 81             
 82         }
 83         //所属大桥下拉列表
 84         public List<Bridge_Info> GetDs()
 85         {
 86             var bridge = BridgeInfoOper.GetBridgeInfoList().ToList();
 87             var list = bridge.AsQueryable().Select(p => p.Bridgename).ToList<string>();
 88             return bridge;
 89         }
 90         //监测项下拉列表
 91         public List<Calc_TypeInfo> GetCt()
 92         {
 93             List<Calc_TypeInfo> obj = new List<Calc_TypeInfo>();
 94             obj = CalcTypeInfoOper.SelectALLCTI();
 95             return obj.ToList();
 96         }
 97         //间隔时间下拉列表
 98         public List<Active_time> GetAct()
 99         {
100             List<Active_time> act = new List<Active_time>();
101             act = MonitorsendlistOper.SelectAct();
102             return act;
103         }
104         //是否允许用户修改
105         public List<string> GetMr()
106         {
107             List<string> strlist = new List<string>();
108             strlist.Add("");
109             strlist.Add("");
110             return strlist;
111             
112         }
113         //取消
114         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
115         {
116             GridView1.EditIndex = -1;
117             BindData();
118         }
119         //删除一行
120         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
121         {            
122             string id = GridView1.DataKeys[e.RowIndex][0].ToString();
123             MonitorsendlistOper.DelmslByID(id);
124             BindData();
125         }       
126         
127         //新增按钮
128         protected void btnAdd_Click(object sender, EventArgs e)
129         {
130             List<Monitor_send_list> objmsl = new List<Monitor_send_list>();
131             objmsl = MonitorsendlistOper.SelectMsl();
132             Monitor_send_list mslnew = new Monitor_send_list();
133             string nid = Guid.NewGuid().ToString();
134             mslnew.MonitorsendID = nid;
135             objmsl.Add(mslnew);
136             this.GridView1.DataSource = objmsl;
137             this.GridView1.DataKeyNames = new string[] { "MonitorsendID" };
138             this.GridView1.EditIndex = objmsl.Count() - 1;
139             this.GridView1.DataBind();
140         }
141 
142 
143 
144 
145        
146         /// <summary>
147         /// 获取新增或编辑后的对象
148         /// </summary>
149         /// <param name="id">对象ID主键</param>
150         /// <param name="rowindex">所处的行号</param>
151         /// <returns></returns>
152         public Monitor_send_list GetMslObj(string id,int rowindex)
153         {
154             try
155             {
156                 
157                 Monitor_send_list mslobj = new Monitor_send_list();
158                 mslobj.MonitorsendID = id;
159                 string sssidd = GridView1.DataKeys[rowindex].ToString();
160                 
161                 DropDownList ddlBridge = (DropDownList)GridView1.SelectedRow.FindControl("lbbridgename");
162                 mslobj.BridgeID = ddlBridge.SelectedValue.ToString();
163                 mslobj.Bridgename = ddlBridge.SelectedItem.Text;
164 
165                 DropDownList ddlCalcType = (DropDownList)GridView1.Rows[rowindex].FindControl("ddlCalcTypeName");
166                 mslobj.CalcTypeID = ddlCalcType.SelectedValue.ToString();
167                 mslobj.CalcTypeName = ddlCalcType.SelectedItem.Text;
168 
169                 DropDownList ddlActiveID = (DropDownList)GridView1.Rows[rowindex].FindControl("ddlActiveID");
170                 mslobj.ActiveID = ddlActiveID.SelectedItem.Text;
171 
172                 mslobj.Monitorsendnumber = Convert.ToInt16(((TextBox)GridView1.Rows[rowindex].FindControl("tbMonitorsendnumber")).Text);
173 
174                 DropDownList ddlMonitorsendrauth = (DropDownList)GridView1.Rows[rowindex].FindControl("ddlMonitorsendrauth");
175                 mslobj.Monitorsendrauth = Convert.ToInt16(ddlMonitorsendrauth.SelectedValue);
176                 return mslobj;
177             }
178             catch (Exception ex)
179             {
180                 throw ex;
181             }
182             
183         }
184 
185         protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
186         {
187             //BindData();  
188         }
189 
190         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
191         {
192             try
193             {
194                 Monitor_send_list mslobj = new Monitor_send_list();
195                 string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
196                 int rowindex = e.RowIndex;
197                 mslobj.MonitorsendID = id;
198                 this.GridView1.SelectRow(rowindex);
199 
200                 DropDownList ddlbridgename = this.GridView1.Rows[e.RowIndex].FindControl("ddlbridgename") as DropDownList;//(DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlCalcTypeName");
201                 mslobj.BridgeID = ddlbridgename.SelectedValue.ToString();
202                 mslobj.Bridgename = ddlbridgename.SelectedItem.Text;
203 
204 
205                 DropDownList ddlCalcType = this.GridView1.Rows[e.RowIndex].FindControl("ddlCalcTypeName") as DropDownList;//(DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlCalcTypeName");
206                 mslobj.CalcTypeID = ddlCalcType.SelectedValue.ToString();
207                 mslobj.CalcTypeName = ddlCalcType.SelectedItem.Text;
208 
209                 DropDownList ddlActiveID = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlActiveID");
210                 mslobj.ActiveID = ddlActiveID.SelectedItem.Text;
211 
212                 mslobj.Monitorsendnumber = Convert.ToInt16(((TextBox)GridView1.Rows[e.RowIndex].FindControl("tbMonitorsendnumber")).Text);
213 
214                 DropDownList ddlMonitorsendrauth = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlMonitorsendrauth");
215                 mslobj.Monitorsendrauth = Convert.ToInt16(ddlMonitorsendrauth.SelectedValue);
216 
217                 MonitorsendlistOper.UpdatemslByObj(mslobj);
218                 GridView1.EditIndex = -1;                
219                 BindData();                
220             }
221             catch (Exception ex)
222             {
223                 throw ex;
224             }
225            
226         }      
227         
228 
229 
230     }
231 }

以下是页面主要代码

<div id="grv">
            <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" 
                OnRowEditing="GridView1_RowEditing" 
                AutoGenerateColumns="False" 
                OnRowCancelingEdit="GridView1_RowCancelingEdit" 
                AllowCustomPaging="True" AllowPaging="True" CssClass="gridview" 
                OnRowDeleting="GridView1_RowDeleting"                
                DataKeyNames="MonitorsendID" EnableViewState="False" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating">
                <Columns>
                    
                    <asp:TemplateField>
                        <HeaderTemplate>MID</HeaderTemplate>
                        <ItemTemplate>                            
                            <%#Eval("MonitorsendID") %>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField>
                        <HeaderTemplate>BID</HeaderTemplate>
                        <ItemTemplate>
                            <%#Eval("BridgeID") %>
                        </ItemTemplate>
                    </asp:TemplateField>     
                    <asp:TemplateField>
                        <HeaderTemplate>序号</HeaderTemplate>                        
                        <ItemTemplate><%#Container.DataItemIndex+1%></ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                        <HeaderTemplate>所属大桥</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lbbridgename" Text='<%#Eval("Bridgename") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlbridgename" runat="server" DataSource='<%#GetDs() %>' DataTextField="Bridgename" DataValueField="BridgeID"></asp:DropDownList>  <%--DataSource='<%#GetDs() %>' DataTextField="Bridgename" DataValueField="BridgeID"--%>
                        </EditItemTemplate>
                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:TemplateField>


                    <asp:TemplateField>
                        <HeaderTemplate>CID</HeaderTemplate>
                        <ItemTemplate>
                            <%#Eval("CalcTypeID") %>
                        </ItemTemplate>
                    </asp:TemplateField>                
                    <%--<asp:BoundField DataField="CalcTypeID" HeaderText="CID" ReadOnly="True" Visible="true" />--%>

                    <asp:TemplateField>
                        <HeaderTemplate>监测项</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lbCalcTypeName" Text='<%#Eval("CalcTypeName") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlCalcTypeName" runat="server" DataSource='<%#GetCt() %>' DataTextField="Name" DataValueField="ID"></asp:DropDownList>
                        </EditItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField>
                        <HeaderTemplate>间隔时间</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lbActiveID" Text='<%#Eval("ActiveID") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlActiveID" runat="server">
                                <asp:ListItem>1 h</asp:ListItem>
                                <asp:ListItem>1 day</asp:ListItem>
                                <asp:ListItem>1 week</asp:ListItem>
                                <asp:ListItem>1 month</asp:ListItem>
                                <asp:ListItem>1 year</asp:ListItem>
                            </asp:DropDownList> <%--DataSource='<%#GetAct() %>' DataTextField="ActiveUnit" DataValueField="ActiveID">--%>
                        </EditItemTemplate>
                    </asp:TemplateField>


                    <asp:TemplateField>
                        <HeaderTemplate>最多发送条数</HeaderTemplate>
                        <ItemTemplate>
                            <%#Eval("Monitorsendnumber") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="tbMonitorsendnumber" runat="server" Text='<%# Bind("Monitorsendnumber") %>'></asp:TextBox>
                            <%--<asp:TextBox ID="txtdName" runat="server" Text='<%# Bind("dName") %>'></asp:TextBox>--%>
                        </EditItemTemplate>
                    </asp:TemplateField>     
                    <%--<asp:BoundField DataField="Monitorsendnumber" HeaderText="最多发送条数" ReadOnly="false" Visible="true" HeaderStyle-HorizontalAlign="Center">
                    </asp:BoundField>--%>

                    <asp:TemplateField>
                        <HeaderTemplate>是否允许用户修改</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lbMonitorsendrauth" Text='<%#Eval("Monitorsendrauth") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlMonitorsendrauth" runat="server" DataTextField="name" DataValueField="id">
                                <%--DataSource='<%#GetMr() %>'  DataTextField="Monitorsendrauth" DataValueField="Monitorsendrauth">--%>
                                <asp:ListItem Value=0>否</asp:ListItem>
                                <asp:ListItem Value=1>是</asp:ListItem>
                            </asp:DropDownList>

                        </EditItemTemplate>
                    </asp:TemplateField>

                    <asp:CommandField ShowEditButton="True"  HeaderText="操作" CausesValidation="false" />
                    <asp:CommandField ShowDeleteButton="True" HeaderText="操作" />
                </Columns>

            </asp:GridView>
        </div>

如题调试在200行一直报索引超出范围,gridview自带的删除没问题,点击就删掉了,就是更新一直不行

1.DropDownList ddlbridgename = this.GridView1.Rows[e.RowIndex].Cells[3].FindControl("ddlbridgename") as DropDownList;

2.DropDownList ddlbridgename = this.GridView1.Rows[e.RowIndex].FindControl("ddlbridgename") as DropDownList;

折两种写法都试过了

夜蓝星希的主页 夜蓝星希 | 菜鸟二级 | 园豆:202
提问于:2015-08-17 10:32
< >
分享
所有回答(1)
0

需要DropDownList ddlbridgename = this.GridView1.Rows[e.RowIndex-1].FindControl("ddlbridgename") as DropDownList吗?

稳稳的河 | 园豆:4216 (老鸟四级) | 2015-08-17 12:12

string dididi = ((DropDownList)this.GridView1.Rows[e.RowIndex].FindControl("ddlbridgename")).SelectedValue.ToString();

写成这样,也报同样的错

支持(0) 反对(0) 夜蓝星希 | 园豆:202 (菜鸟二级) | 2015-08-17 12:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册