(新手.net 2.0提问)如何修改DataView的数据更新到数据源?
页面上有一个DataView,他绑定到 userName 列:
DataView1.DataSource = DataTable1.DefaultView;
同时DataView内还有一个“选择”按钮,用ButtonField创建。
DataView下方有个文本框(txb1)和一个“修改”(btn1)按钮。
====================================
现在我要做的是,按下选择按钮后,txb1显示所选中的userName内容,修改后,按下btn1,修改该数据,然后刷新DataView,该怎么做?谢谢!!
[解决办法]
DataSource 是asp.net1.1以前的设计,你应该多看看 DataSourceID 相关介绍(包括bind“双向”绑定而不是单向绑定),那里非常自动化,很少需要些代码。保留 DataSource 只是为了兼容,使用这个需要自己写很多代码,你再写程序应该几乎再也不用这个属性。
[解决办法]
/// <summary>
/// 删除事件
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
private void toolStripButtonDelete_Click(object sender, EventArgs e)
{
try
{
loginForm lg = new loginForm();
//当该帐号正在登陆的时候,不能删除该帐号。
for (int j = 0; j < dataGridView1.SelectedRows.Count; j++)
{
if (dataGridView1.Rows[dataGridView1.SelectedRows[j].Index].Cells[ "AccoutID "].Value.ToString() == lg.UserID)
{
MessageBox.Show(ResourceFactory.GetMultilanguage( "NOTALLOWDELETE "));
return;
}
}
MessageBoxButtons Buttons = MessageBoxButtons.OKCancel;
DialogResult Result;
Result = MessageBox.Show(ResourceFactory.GetMultilanguage( "DELASKNEW "), ResourceFactory.GetMultilanguage( "ENSUREDELETE "), Buttons, MessageBoxIcon.Question);
if (Result == DialogResult.OK)
{
//row = dataGridView1.CurrentRow;
if (dataGridView1.SelectedRows.Count > 0)
{
DataFactory dtfactory = new DataFactory( "AlarmSystem ");
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
//同时删除掉TUserAccount,TNoticeUser,TNoticeGroupLink中相关联的数据。
string UserID = dataGridView1.Rows[dataGridView1.SelectedRows[i].Index].Cells[ "UserID "].Value.ToString();
string sql = string.Format( "delete from TUserAccount where UserID = '{0} ' "+
" delete from TNoticeUser where UserID = '{1} ' "+
" delete from TNoticeUser where UserID = '{2} ' ", UserID, UserID, UserID);
dtfactory.UpDataRecord(sql);
}
MessageBox.Show(ResourceFactory.GetMultilanguage( "DELETEGROUPSUCCESS "));
//selectindex = selectindex - 1;
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
dataGridView1.Rows[dataGridView1.SelectedRows[i].Index].HeaderCell.ToolTipText = "* ";
}
//dataGridView中同时删除多笔
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].HeaderCell.ToolTipText == "* ")
{
dataGridView1.Rows.RemoveAt(i);
}
}
//重新读取数据库以获得最新资料
ReadUserTable();
if (dataGridView1.Rows.Count > 0)
{
row = dataGridView1.CurrentRow;
this.txtUserID.Text = row.Cells[ "AccoutID "].Value.ToString();
}
else
{
this.txtUserID.Text = " ";
}
}
else
{
MessageBox.Show(ResourceFactory.GetMultilanguage( "SELECTDELETE "));
}
}
}
catch (Exception ex)
{
LogEntry log = new LogEntry();
log.Message = ex.Message;
Dictionary <string, object> extendInfo = new Dictionary <string, object> ();
extendInfo.Add( "DateTime ", DateTime.Now);
log.ExtendedProperties = extendInfo;
log.Categories.Add( "Error ");
Logger.Write(log);
}
}