为什么我的代码不能取得DataGrid(WebForm)中处于编辑状态的单元格的值。

ghbh 2004-05-12 07:56:24
在属性生成器中添加编辑按纽,点编辑按纽后在编辑框中输入新值。然后用下面的代码。
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TextBox ControlText1,ControlText2;
ControlText1 = (TextBox)e.Item.Cells[1].Controls[0];
ControlText2 = (TextBox)e.Item.Cells[2].Controls[0];
this.Response.Write(ControlText1.Text);
}
this.Response.Write(ControlText1.Text);输出的还是原来的值,为什么?

...全文
148 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiusong1983 2004-06-10
  • 打赏
  • 举报
回复
我也碰到这个问题,碰巧楼上的方法我也用了。可惜问题更严重,挑过了databind连EditCommand都无法正常显示,更别说update了
whatsup 2004-06-06
  • 打赏
  • 举报
回复
...

private void Page_Load(object sender, System.EventArgs e){
// 在这里加上判断语句
if(! Page.IsPostBack){
// 在此处放置用户代码以初始化页面
string OptCommand;
...
ExampleDB.Conn.Close();

}// end of if(! Page.IsPostBack)
}

...
ghbh 2004-05-14
  • 打赏
  • 举报
回复
我的代码:
public class viewplan : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.DataTable DataTable;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string OptCommand;
OptCommand = "Select * From Plan_List";
Audit.Data.DB ExampleDB = new Audit.Data.DB();
ExampleDB.dbOpen();
DataSet ExampleDS = new DataSet();
SqlDataAdapter ExampleDA = new SqlDataAdapter(OptCommand,ExampleDB.Conn);
ExampleDA.Fill(ExampleDS, "DSPlan_List");
DataTable = ExampleDS.Tables["DSPlan_List"];
DataGrid1.DataSource = DataTable;
DataGrid1.DataBind();
ExampleDB.Conn.Close();
}

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//DataGrid1.CurrentPageIndex = e.NewPageIndex;
//DataGrid1.DataBind();
}

private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.DataSource = DataTable;
DataGrid1.EditItemIndex = e.Item.ItemIndex;

DataGrid1.DataBind();
}

private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string ControlText1 = ((TextBox)e.Item.Cells[1].Controls[0]).Text.ToString();
//string ControlText2 = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
this.Response.Write(ControlText1 );//这里取不出新输入的值

return;
}

private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
ghbh 2004-05-14
  • 打赏
  • 举报
回复
To All
我用了大家说的所有方法都取不出在编辑框中新输入的值,而且把编辑按钮类型从LinkButton改为PushButton后,点编辑后DataGrid不能进入编辑状态。是不是机器有问题.
rainman1981 2004-05-14
  • 打赏
  • 举报
回复
点击编辑后
有一个属性会改变,就是
DataGrid1.EditItemIndex,这个索引值就是当前编辑行的值

可以用以下语句取得编辑后的数值
string[] temp={};
for(int i=0;i<columnCount-1;i++)
{
temp[i] = (TextBox)DataGrid1.Items[DataGrid1.EditItemIndex].Cells[i].Controls[0]).Text
}
然后用insert语句插入数据库
ghbh 2004-05-13
  • 打赏
  • 举报
回复
To hychieftain(不同)
你没有理解我的意思,我的意思是取不出来新输入的值。
eTopFancy 2004-05-13
  • 打赏
  • 举报
回复
如果你不还行移动一下,即让datagrid自己将数据保存到DataSource中,你的编辑始终是无效的,即使你用((DataTable)dataGrid1.DataSource).AcceptChanges(),原来的数据还是不变
eTopFancy 2004-05-13
  • 打赏
  • 举报
回复
你可以将焦点已到其他行,然后再回到那个单元,这个时候你取道的值应该是新值了,这是datagrid的机制问题
hychieftain 2004-05-13
  • 打赏
  • 举报
回复
to ghbh(我想编但是编不出来)
更新了DataSet啊,在UpdateView的da.Fill(ds, "MyTable"); 语句就更新了

ghbh 2004-05-13
  • 打赏
  • 举报
回复
TO graybelt(灰色地带)
结果一样。
那位帮我调试一下。
graybelt 2004-05-12
  • 打赏
  • 举报
回复
不要用TEXTBOX类型
用STRING类型
string ControlText1 = ((TextBox)e.Item.Cells[1].Controls[0].text;
ghbh 2004-05-12
  • 打赏
  • 举报
回复
To bitsbird(一瓢.net)
意思是:比如列[1]现在的值是aaa,当你点编辑按钮时,DataGrid进入编辑状态,你在列[1]中输入新值bbb,(TextBox)e.Item.Cells[1].Controls[0]取出的值还是aaa。为什么?

To hychieftain(不同)
你的代码我看过,你说没有更新dataset,可是像我这样的情况取不出来新输入的值怎么更新DataSet呢?
huangsuipeng 2004-05-12
  • 打赏
  • 举报
回复
呵呵,顶吧
hychieftain 2004-05-12
  • 打赏
  • 举报
回复
楼主输出的还是原来的值,是因为没有更新dataset吧
更详细的示例可以给我短信留你的e-mail地址,我发给你
hychieftain 2004-05-12
  • 打赏
  • 举报
回复
楼主的代码没有更新DataGrid及绑定的Data ,下面有示例代码
public void UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
// Retrieve the new text
int nColPositionIndex = 2; // 0-based position of the column
int nColFromIndex = 3; // 0-based position of the column
TextBox txtPosition = (TextBox) e.Item.Cells[nColPositionIndex].Controls[0];
TextBox txtFrom = (TextBox) e.Item.Cells[nColFromIndex].Controls[0];

// Prepare the command text
String strCmd = "UPDATE Employees SET title=@sPosition, country=@sCountry WHERE employeeid=@nEmpID";
SqlConnection conn = new SqlConnection(txtConn.Text);
SqlCommand cmd = new SqlCommand(strCmd, conn);

SqlParameter p1 = new SqlParameter("@nEmpID", SqlDbType.Int);
p1.Direction = ParameterDirection.Input;
p1.Value = grid.DataKeys[e.Item.ItemIndex];
cmd.Parameters.Add(p1);

SqlParameter p2 = new SqlParameter("@sPosition", SqlDbType.NVarChar, 30);
p2.Direction = ParameterDirection.Input;
p2.Value = txtPosition.Text;
cmd.Parameters.Add(p2);

SqlParameter p3 = new SqlParameter("@sCountry", SqlDbType.NVarChar, 15);
p3.Direction = ParameterDirection.Input;
p3.Value = txtFrom.Text;
cmd.Parameters.Add(p3);

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();


// Reset the edit mode for the current item
grid.EditItemIndex = -1;

// Refresh the grid
UpdateView();
}

private void UpdateView()
{
SqlConnection conn = new SqlConnection(txtConn.Text);
SqlDataAdapter da = new SqlDataAdapter(txtCommand.Text, conn);

DataSet ds = new DataSet();
da.Fill(ds, "MyTable");

// Bind the data
grid.DataSource = ds.Tables["MyTable"];

// Display the data
grid.DataBind();
}
bitsbird 2004-05-12
  • 打赏
  • 举报
回复
输出的还是原来的值
-----=----------------
什么意思?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧