晕死,如何取得GridView1_RowEditing事件里当前行各列的值
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e){
GridViewRow row = GridView1.SelectedRow;
int selectIndex = GridView1.SelectedIndex;
txtmenuid.Value = "001";
txtmenuname.text = GridView1.SelectedRow.Cells[1].Text.ToString();
}
执行到这句 txtmenuname.text = GridView1.SelectedRow.Cells[1].Text.ToString();时出错
很是困惑,发觉在不同的事件里取值的方法不相同,没有一种通用的方法实现在Gridview取得当前行各列的值呢??
--------------------编程问答-------------------- ((TextBox)this.GvwUser.Rows[e.RowIndex].Cells[0].Controls[0]).Text
不过时间好像不太对吧,_RowUpdating --------------------编程问答--------------------
在RowEditing事件里没有e.RowIndex值,但有e.NewRowIndex,你可以试试 --------------------编程问答--------------------
是e.neweditindex --------------------编程问答-------------------- 奇怪,怎么gridview1.selectedrow怎么没有用武之地? --------------------编程问答-------------------- 友情UP! --------------------编程问答-------------------- 运行错误是什么呀????? --------------------编程问答--------------------
错误是:未将对象引用设置到对象的实例。 --------------------编程问答-------------------- 在GridView1的RowEditing事件下应该只能取到ItemTemplate中的Label值
即只能是:
txtmenuname.text =((Label)GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("Label1")).Text; --------------------编程问答--------------------
不是吧????
--------------------编程问答-------------------- 没人会吗? --------------------编程问答-------------------- this.GridView1.EditIndex = e.NewEditIndex; --------------------编程问答--------------------
这个有何用? --------------------编程问答--------------------
UP --------------------编程问答--------------------
这里貌似没用 --------------------编程问答-------------------- 晕,难道真的无法实现? --------------------编程问答-------------------- 在绑定到dataGridView之前,你先使用reader或者dataTable来查找到需要的值不是更好吗?何必非要到dataGridView里面去取? --------------------编程问答--------------------
用这个确定是哪行要使用编辑模板呀
--------------------编程问答-------------------- GridView的编辑行和非编辑行是不一样的,编辑时,由于每列对应的是输入控件,或者是模板列,所以用Cells[i].Text是取不到数据的,要用(Cells[i].Controls[0] as TextBox).Text,如果是模板列,则要用FindControl。
一般的我比较喜欢用模板列,而且是ItemTemplet,即非编辑状态就可以修改数据,然后访问每行组合成DataTable,这样的好处就是用户直接修改就可以不用界面刷新。 --------------------编程问答--------------------
能否给个例子啊? --------------------编程问答-------------------- 这是我以前做的项目中一段,gridview就是全部定义了ItemTemplate
<asp:GridView ID="dbgDetail" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="2" ForeColor="Black"
GridLines="Vertical" ShowFooter="True" OnRowDataBound="dbgDetail_RowDataBound">
<FooterStyle BackColor="#CCCC99" />
<Columns>
<asp:TemplateField HeaderText="行政区划代码">
<ItemTemplate>
<asp:TextBox ID="行政区划代码" runat="server" CssClass="input_2d_int" Text='<%# Bind("行政区划代码") %>'
Width="110px" MaxLength="12"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="行政区划代码" runat="server" CssClass="input_2d_int" MaxLength="12" Text='<%# Bind("行政区划代码") %>'
Width="110px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="顺序码" HeaderText="顺序码" />
<asp:TemplateField HeaderText="合并顺序码">
<ItemTemplate>
<asp:TextBox ID="合并顺序码" runat="server" CssClass="input_2d_int" Text='<%# Bind("合并顺序码") %>'
Width="46px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width=46px/>
<FooterTemplate>
<asp:TextBox ID="合并顺序码" runat="server" CssClass="input_2d_int" Text='<%# Bind("合并顺序码") %>'
Width="46px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="组织机构代码">
<ItemTemplate>
<asp:TextBox ID="组织机构代码" runat="server" CssClass="input_2d" Text='<%# Bind("组织机构代码") %>'
Width="86px"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="组织机构代码" runat="server" CssClass="input_2d" Text='<%# Bind("组织机构代码") %>'
Width="86px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="单位标识">
<ItemTemplate>
<asp:TextBox ID="单位标识" runat="server" CssClass="input_2d_int" Text='<%# Bind("单位标识") %>'
Width="30px" MaxLength="1"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width=30px />
<FooterTemplate>
<asp:TextBox ID="单位标识" runat="server" CssClass="input_2d_int" MaxLength="1" Text='<%# Bind("单位标识") %>'
Width="30px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="单位名称" HeaderText="单位名称" />
<asp:BoundField DataField="详细地址" HeaderText="详细地址" />
<asp:TemplateField HeaderText="建筑物编码">
<ItemTemplate>
<asp:TextBox ID="建筑物编码" runat="server" CssClass="input_2d" MaxLength="50" Text='<%# Bind("建筑物编码") %>'
Width="45px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="45px" />
<FooterTemplate>
<asp:TextBox ID="建筑物编码" runat="server" CssClass="input_2d" MaxLength="50" Text='<%# Bind("建筑物编码") %>'
Width="45px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="法人">
<ItemTemplate>
<asp:TextBox ID="法定代表人" runat="server" CssClass="input_2d" MaxLength="50" Text='<%# Bind("法定代表人") %>'
Width="45px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width=45px />
<FooterTemplate>
<asp:TextBox ID="法定代表人" runat="server" CssClass="input_2d" MaxLength="50" Text='<%# Bind("法定代表人") %>'
Width="45px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="联系电话">
<ItemTemplate>
<asp:TextBox ID="联系电话" runat="server" CssClass="input_2d" MaxLength="50" Text='<%# Bind("联系电话") %>'
Width="89px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="89px" />
<FooterTemplate>
<asp:TextBox ID="联系电话" runat="server" CssClass="input_2d" MaxLength="50" Text='<%# Bind("联系电话") %>'
Width="89px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="核查情况">
<ItemTemplate>
<asp:TextBox ID="核查情况" runat="server" CssClass="input_2d_int" MaxLength="1" Text='<%# Bind("核查情况") %>'
Width="29px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="29px" />
<FooterTemplate>
<asp:TextBox ID="核查情况" runat="server" CssClass="input_2d_int" MaxLength="1" Text='<%# Bind("核查情况") %>'
Width="29px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="备注">
<ItemTemplate>
<asp:TextBox ID="备注" runat="server" CssClass="input_2d" MaxLength="225" Text='<%# Bind("备注") %>'
Width="111px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="111px" />
<FooterTemplate>
<asp:TextBox ID="备注" runat="server" CssClass="input_2d" MaxLength="225" Text='<%# Bind("备注") %>'
Width="111px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<EmptyDataTemplate>
(无记录)
</EmptyDataTemplate>
</asp:GridView>
--------------------编程问答--------------------
后台绑定就是设置DataSource,关键就是保存时获取各行的值:
string 行政区划代码 = string.Empty, 组织机构代码, 合并顺序码, 单位标识, 顺序码 = "0", 建筑物编码, 法定代表人, 联系电话, 核查情况, 备注;
string 行政区划代码f, 组织机构代码f, 合并顺序码f, 单位标识f, 顺序码f, 建筑物编码f, 法定代表人f, 联系电话f, 核查情况f, 备注f;
string strTmp = string.Empty;
string strErrorMessage = string.Empty;
行政区划代码f = (dbgDetail.FooterRow.Cells[0].Controls[1] as TextBox).Text;
合并顺序码f = (dbgDetail.FooterRow.Cells[2].Controls[1] as TextBox).Text;
组织机构代码f = (dbgDetail.FooterRow.Cells[3].Controls[1] as TextBox).Text;
单位标识f = (dbgDetail.FooterRow.Cells[4].Controls[1] as TextBox).Text;
建筑物编码f = (dbgDetail.FooterRow.Cells[7].Controls[1] as TextBox).Text;
法定代表人f = (dbgDetail.FooterRow.Cells[8].Controls[1] as TextBox).Text;
联系电话f = (dbgDetail.FooterRow.Cells[9].Controls[1] as TextBox).Text;
核查情况f = (dbgDetail.FooterRow.Cells[10].Controls[1] as TextBox).Text;
备注f = (dbgDetail.FooterRow.Cells[11].Controls[1] as TextBox).Text;
foreach (GridViewRow ARow in dbgDetail.Rows)
{
行政区划代码 = (ARow.Cells[0].Controls[1] as TextBox).Text;
顺序码 = ARow.Cells[1].Text;
合并顺序码 = (ARow.Cells[2].Controls[1] as TextBox).Text;
组织机构代码 = (ARow.Cells[3].Controls[1] as TextBox).Text;
单位标识 = (ARow.Cells[4].Controls[1] as TextBox).Text;
建筑物编码 = (ARow.Cells[7].Controls[1] as TextBox).Text;
法定代表人 = (ARow.Cells[8].Controls[1] as TextBox).Text;
联系电话 = (ARow.Cells[9].Controls[1] as TextBox).Text;
核查情况 = (ARow.Cells[10].Controls[1] as TextBox).Text;
备注 = (ARow.Cells[11].Controls[1] as TextBox).Text;
}
--------------------编程问答-------------------- 感觉楼主没有必要在DataGridView里面读取值。建议楼主从DataGridView的数据源DataSet.
可以从DataGridView中读到Index,然后从DataSet数据集里读数据比较好。
--------------------编程问答-------------------- --------------------编程问答-------------------- editing是事前触发的,就是在生成那些个编辑控件之前执行的。
//获取DataSet数据集的数据
DataSet1.Tables[0].Rows[i][j].ToString();
你可以试试databound事件,在其中判断gridview的状态 进而获取编辑控件数据 --------------------编程问答-------------------- int index = e.NewEditIndex;
string SxUsername = GridView1.Rows[index].Cells[1].Text;提取所选行的第一列的值
补充:.NET技术 , C#