您的位置:首页 > 移动开发 > Objective-C

新手.net 2.0提问)如何修改DataView的数据更新到数据源?

2008-04-07 22:02 453 查看
新手.net 2.0提问)如何修改DataView的数据更新到数据源?
添加时间:2007-6-4 22:48:31 ,点击量:

11
<script type="text/javascript"><!--
google_ad_client = "pub-1166618656214890";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel = "";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "008000";
//-->
</script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script>
<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1166618656214890&dt=1207576639839&lmt=1207576639&format=728x90_as&output=html&correlator=1207576639380&url=http%3A%2F%2Fwww.dj9158.com%2FCsdnShow%2F28227.html&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=008000&color_border=FFFFFF&ad_type=text_image&ref=http%3A%2F%2Fwww.dj9158.com%2FCsdnShow%2F458.html&frm=0&cc=100&ga_vid=119495127.1207576640&ga_sid=1207576640&ga_hid=1605770390&flash=9.0.47.0&u_h=800&u_w=1280&u_ah=769&u_aw=1280&u_cd=32&u_tz=480&u_his=1&u_java=true" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="allowtransparency"></iframe>

问:
页面上有一个DataView,他绑定到 userName 列:

DataView1.DataSource = DataTable1.DefaultView;

同时DataView内还有一个“选择”按钮,用ButtonField创建。

DataView下方有个文本框(txb1)和一个“修改”(btn1)按钮。

====================================
现在我要做的是,按下选择按钮后,txb1显示所选中的userName内容,修改后,按下btn1,修改该数据,然后刷新DataView,该怎么做?谢谢!!

回答1:
dataset

回答2:
给100的原因是我理解能力差,麻烦楼上说清楚点。

回答3:
DataSource 是asp.net1.1以前的设计,你应该多看看 DataSourceID 相关介绍(包括bind“双向”绑定而不是单向绑定),那里非常自动化,很少需要些代码。保留 DataSource 只是为了兼容,使用这个需要自己写很多代码,你再写程序应该几乎再也不用这个属性。

回答4:
DS有这个功能的吧

回答5:

/// 新增事件
private void toolStripButtonNewUser_Click(object sender, EventArgs e)
{
string sql = "";
string newPassword = UIMisc.EncryptString(this.txtPassword.Text);
string tempPassword = UIMisc.EncryptString(this.txtUserID.Text);
bool check = this.CheckPK(this.txtUserID.Text);
if (CheckNoNull() == true)
{
return;
}
if (InputValidations() == false)
{
return;
}
if (check == true)
{
//帐户已存在
MessageBox.Show(ResourceFactory.GetMultilanguage("USEREXIST"));
this.txtUserID.Text = "";
}
else
{
if (this.txtUserID.Text == "" || this.txtUserID.Text == null)
{
//帐户不可为空
MessageBox.Show(ResourceFactory.GetMultilanguage("USERIDNOTNULL"));
}
else
{
int userID = InsertTNoticeUser();
//权限为空时
if (this.txtGroupID.Text == "" || this.txtGroupID.Text == null)
{
//密码栏位显示且不为空时
if (txtPassword.Visible == true && this.txtPassword.Text != "")
{
sql = string.Format("insert into TUserAccount (AccoutID,UserID,password,UserGroupID) values ('{0}','{1}','{2}','-1')", txtUserID.Text, userID, newPassword);
}
//密码栏位显示且为空时
if (txtPassword.Visible == true && this.txtPassword.Text == "")
{
MessageBox.Show(ResourceFactory.GetMultilanguage("PASSWORDNOTNULL"));
return;
}
//密码栏位不显示时
if (txtPassword.Visible == false)
{
sql = string.Format("insert into TUserAccount (AccoutID,UserID,password,UserGroupID) values ('{0}','{1}','{2}','-1')", txtUserID.Text, userID, tempPassword);
}
}
else
{
//密码栏位显示且不为空时
if (txtPassword.Visible == true && this.txtPassword.Text != "")
{
sql = string.Format("insert into TUserAccount (AccoutID,UserID,password,UserGroupID) values ('{0}','{1}','{2}','{3}')", txtUserID.Text,userID, newPassword, userGroupID);
}
//密码栏位显示且为空时
if (txtPassword.Visible == true && this.txtPassword.Text == "")
{
MessageBox.Show(ResourceFactory.GetMultilanguage("PASSWORDNOTNULL"));
return;
}
//密码栏位不显示时
if (txtPassword.Visible == false)
{
sql = string.Format("insert into TUserAccount (AccoutID,UserID,password,UserGroupID) values ('{0}','{1}','{2}','{3}')", txtUserID.Text, userID, tempPassword, userGroupID);
}

}
try
{
DataFactory dtfactory = new DataFactory("AlarmSystem");
dtfactory.UpDataRecord(sql);

this.txtPassword.Visible = false;
TxtPasswordState();
ReadUserTable();
}
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);
}
//把光标置于新增的位置
int index = CheckRowsCount(this.txtUserID.Text);
dataGridView1.CurrentCell = dataGridView1["AccoutID", index];

this.txtUserID.Text = "";
this.txtJob.Text = "";
this.txtMail.Text = "";
this.txtNotesMail.Text = "";
this.txtPhone.Text = "";
this.txtMobile.Text = "";
this.txtUserName.Text = "";
this.txtGroupID.Text = "";
}
}
}

回答6:
/// <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);
}
}

回答7:
/// <summary>
/// 修改事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButtonModifyUser_Click(object sender, EventArgs e)
{
string sql = "";
string userID = "";
row = dataGridView1.CurrentRow;
string newPassword = "";
string newUserID = "";
//判断是否为空
if (CheckNoNull() == true)
{
return;
}
//判断Email,电话栏位格式
if (InputValidations() == false)
{
return;
}
try
{
DataFactory dtfactory = new DataFactory("AlarmSystem");

        //当选中的行数为一行时才执行修改动作。
if (dataGridView1.SelectedCells.Count == 1 || dataGridView1.SelectedRows.Count == 1)
{

if (this.txtUserID.Text.ToString() == row.Cells["AccoutID"].Value.ToString())
{
string CheckUserID = string.Format("select * from TUserAccount where AccoutID = '{0}'", this.txtUserID.Text);
bool tempUserID = dtfactory.FetchDataReader(CheckUserID);
if (tempUserID == false)
{
//帐号已经存在
MessageBox.Show(ResourceFactory.GetMultilanguage("USERIDISEXIT"));
ReadUserTable();
}
else
{
//当密码栏未不可见的时候把初始密码保存进数据库,反之把用户输入的密码加密后存进数据库。
newPassword = row.Cells["Password"].Value.ToString();
newUserID= row.Cells["UserID"].Value.ToString();
//加密密码
if (this.txtPassword.Visible == true && this.txtPassword.Text != "")
{
newPassword = UIMisc.EncryptString(this.txtPassword.Text);
}
//密码栏未可见,但是为空的时候提示
if (this.txtPassword.Visible == true && this.txtPassword.Text == "")
{
//密码不可为空
MessageBox.Show(ResourceFactory.GetMultilanguage("PASSWORDNOTNULL"));
return;
}
//当权限为空时,把该栏位值付为“-1”
if (this.txtGroupID.Text == null || this.txtGroupID.Text == "")
{
row = dataGridView1.CurrentRow;
sql = string.Format("Update TUserAccount set Password = '{0}',UserGroupID ='-1',UserID = '{1}' where AccoutID = '{2}'" +
" Update TNoticeUser set UserName = '{3}',Job = '{4}',NotesMail = '{5}',Phone = '{6}',Mobile = '{7}',Email = '{8}'" +
"where UserID = '{9}'", newPassword, newUserID, txtUserID.Text,txtUserName.Text,txtJob.Text,txtNotesMail.Text,txtPhone.Text,txtMobile.Text,txtMail.Text,row.Cells["UserID"].Value.ToString());
}
//不为空时把该栏位值对应的GroupID存进帐户表
else
{
string userIDSql = string.Format("select GroupID from TUserGroup where GroupName = '{0}'", txtGroupID.Text);
try
{
userID = dtfactory.FetchScalar(userIDSql);
}
catch
{
MessageBox.Show(ResourceFactory.GetMultilanguage("GROUPNAMEEXIT"));
this.txtGroupID.Text = "";
return;
}
row = dataGridView1.CurrentRow;
sql = string.Format("Update TUserAccount set Password = '{0}',UserGroupID ='{1}',UserID = '{2}' where AccoutID = '{3}'" +
" Update TNoticeUser set UserName = '{4}',Job = '{5}',NotesMail = '{6}',Phone = '{7}',Mobile = '{8}',Email = '{9}'" +
"where UserID = '{10}'", newPassword, userID, newUserID, txtUserID.Text, txtUserName.Text, txtJob.Text, txtNotesMail.Text, txtPhone.Text, txtMobile.Text, txtMail.Text, row.Cells["UserID"].Value.ToString());
}
dtfactory.UpDataRecord(sql);
MessageBox.Show(ResourceFactory.GetMultilanguage("UPDATESUCCESS"));
this.txtPassword.Text = null;
this.txtPassword.Visible = false;
TxtPasswordState();
}
}
else
{
if (this.txtUserID.Text == "" || this.txtUserID.Text == null)
{
MessageBox.Show(ResourceFactory.GetMultilanguage("USERIDNOTNULL"));
}
else
{
MessageBox.Show(ResourceFactory.GetMultilanguage("USERIDNOTMODIFY"));
//把光标置于当前修改的地方
row = dataGridView1.CurrentRow;
this.txtUserID.Text = row.Cells["AccoutID"].Value.ToString();
return;
}

}
}
else
{
MessageBox.Show(ResourceFactory.GetMultilanguage("SELECTUPDATECOUNT"));
}
}
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);
}
ReadUserTable(); ;
row = dataGridView1.CurrentRow;
this.txtUserID.Text = row.Cells["AccoutID"].Value.ToString();
this.txtPhone.Text = row.Cells["Phone"].Value.ToString();
this.txtMail.Text = row.Cells["EMail"].Value.ToString();
this.txtMobile.Text = row.Cells["Mobile"].Value.ToString();
this.txtNotesMail.Text = row.Cells["NotesMail"].Value.ToString();
this.txtJob.Text = row.Cells["Job"].Value.ToString();
this.txtJob.Text = row.Cells["Job"].Value.ToString();

this.txtUserName.Text = row.Cells["UserName"].Value.ToString();

}

回答8:
能不能给个像我提供的那样简单的范例?我只是想学习下,谢谢

回答9:


回答10:
没人回答吗?

回答11:
需求不是很明白,帮UP了

回答12:
 我也没看明白``还有 我很不习惯 0代码编程```
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐