如何使用行编辑编辑所有网格视图列,而不使用sqldatasource [英] how to edit all grid view columns using row editing and without using sqldatasource

查看:94
本文介绍了如何使用行编辑编辑所有网格视图列,而不使用sqldatasource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中有两个网格视图,一个用于添加列,另一个用于编辑和更新

webform.aspx



< asp:GridView ID =" GVDETAILS" RUNAT = QUOT;服务器" AutoGenerateColumns =" false"

onrowcommand =" GVDETAILS_RowCommand" OnRowEditing =" GVUPDATE_RowEditing"

onrowupdating =" GVDETAILS_RowUpdating"

onselectedindexchanged =" GVDETAILS_SelectedIndexChanged">

< Columns>

< asp:TemplateField HeaderText =" Name">

< ItemTemplate>

< asp:TextBox ID =" TxtName" runat =" server">

< / asp:TextBox>

< / ItemTemplate>

< / asp:TemplateField> ;

< asp:TemplateField HeaderText =" GENDER">

< ItemTemplate>

< asp:DropDownList ID =" ; DDLGender" runat =" server">

< asp:ListItem Value =" 0" Text =" MALE"

< / asp:ListItem>

< asp:ListItem Value =" 1" Text =" FEMALE">< / asp:ListItem>

< / asp:DropDownList>

< / ItemTemplate>

< / asp:TemplateField>

< asp:TemplateField HeaderText =" RESULT">

< ItemTemplate>

< ; asp:RadioButton ID =" RB"文本= QUOT; PASS" RUNAT = QUOT;服务器" />

< asp:RadioButton ID =" RB1"文本= QUOT; FAIL" RUNAT = QUOT;服务器" />

< / ItemTemplate>

< / asp:TemplateField>

< asp:ButtonField ButtonType ="按钮"文本= QUOT;新增]的CommandName = QUOT;新增] />

< / Columns>

< / asp:GridView>

< asp:GridView ID =" GVUPDATE" RUNAT = QUOT;服务器" onrowediting ="" GVUPDATE_RowEditing">< / asp:GridView>

< asp:Button ID =" btnedit" RUNAT = QUOT;服务器"文本= QUOT; EDITALL" />

< asp:Button ID =" btnupdate" RUNAT = QUOT;服务器"文本= QUOT; UPADTEALL"宽度= QUOT; 103px" />

< / div>

< / form>

< / body>

< / html>



weform.cs文件:

protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{

DataTable _dt = new DataTable();

_dt .TableName =Details;

_dt.Columns.Add(Name,typeof(string));

_dt.Columns.Add(Gender,typeof (string));

_dt.Columns.Add(Result,typeof(string));

DataRow _dr;

_dr = _dt.NewRow();

_dt.Rows.Add(_dr);

ViewState [Details] = _dt;

GVDETAILS。 DataSource = _dt;

GVDETAILS.DataBind();

}



}

protected void GVUPDATE_RowEditing(object sender,GridViewEditEventArgs e)

{

GVUPDATE.EditIndex = e.NewEditIndex;



GVUPDATE.DataBind();

}

protected void GVDETAILS_RowCommand(object sender,GridViewCommandEventArgs e)

{

if(e.CommandName ==ADD)

{

if(ViewState [Details]!= null)

{

DataTable dt =(DataTable)ViewState [Details];

DataRow dr = dt.NewRow();

int index = Convert.ToInt32(e.CommandArgument.ToString());

GridViewRow gvr = GVDETAILS.Rows [index];

dr [0] =((TextBox )gvr.FindControl(TxtName))。文本;

dr [1] =((DropDownList)gvr.FindControl(DDLGender))。SelectedValu e;

if(((RadioButton)gvr.FindControl(RB))。选中== true)

{

dr [ 2] =通过;

}

其他

{

dr [2] =失败 ;

}

dt.Rows.Add(dr);

GVUPDATE.DataSource = dt;

GVUPDATE .DataBind();

}

}

}

问题:只有一行正在编辑...所有其他行都无法编辑

plzz帮助我...提前感谢

In My Web Application There are two grid view's one is for adding columns another is for editing and updating
webform.aspx

<asp:GridView ID ="GVDETAILS" runat="server" AutoGenerateColumns="false"
onrowcommand="GVDETAILS_RowCommand" OnRowEditing="GVUPDATE_RowEditing"
onrowupdating="GVDETAILS_RowUpdating"
onselectedindexchanged="GVDETAILS_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="TxtName" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GENDER">
<ItemTemplate>
<asp:DropDownList ID="DDLGender" runat="server">
<asp:ListItem Value="0" Text="MALE">
</asp:ListItem>
<asp:ListItem Value="1" Text="FEMALE"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RESULT">
<ItemTemplate>
<asp:RadioButton ID="RB" Text="PASS" runat="server" />
<asp:RadioButton ID ="RB1" Text="FAIL" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" Text="ADD" CommandName="ADD" />
</Columns>
</asp:GridView>
<asp:GridView ID="GVUPDATE" runat="server" onrowediting="GVUPDATE_RowEditing"></asp:GridView>
<asp:Button ID="btnedit" runat="server" Text="EDITALL" />
<asp:Button ID="btnupdate" runat="server" Text="UPADTEALL" Width="103px" />
</div>
</form>
</body>
</html>

weform.cs file:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable _dt = new DataTable();
_dt.TableName = "Details";
_dt.Columns.Add("Name", typeof(string));
_dt.Columns.Add("Gender", typeof(string));
_dt.Columns.Add("Result", typeof(string));
DataRow _dr;
_dr = _dt.NewRow();
_dt.Rows.Add(_dr);
ViewState["Details"] = _dt;
GVDETAILS.DataSource = _dt;
GVDETAILS.DataBind();
}

}
protected void GVUPDATE_RowEditing(object sender, GridViewEditEventArgs e)
{
GVUPDATE.EditIndex = e.NewEditIndex;

GVUPDATE.DataBind();
}
protected void GVDETAILS_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ADD")
{
if (ViewState["Details"] != null)
{
DataTable dt = (DataTable)ViewState["Details"];
DataRow dr = dt.NewRow();
int index = Convert.ToInt32(e.CommandArgument.ToString());
GridViewRow gvr = GVDETAILS.Rows[index];
dr[0] = ((TextBox)gvr.FindControl("TxtName")).Text;
dr[1] = ((DropDownList)gvr.FindControl("DDLGender")).SelectedValue;
if (((RadioButton)gvr.FindControl("RB")).Checked == true)
{
dr[2] = "pass";
}
else
{
dr[2] = "fail";
}
dt.Rows.Add(dr);
GVUPDATE.DataSource = dt;
GVUPDATE.DataBind();
}
}
}
PROBLEM:Only one row is editing...all other rows can't been edited
plzz help me... thanks in advance

推荐答案

试试这个..



1) Gridview编辑在ASP.NET中删除和更新


这篇关于如何使用行编辑编辑所有网格视图列,而不使用sqldatasource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆