以编程方式更新详细信息查看 C#

发布于 2024-10-03 22:44:39 字数 157 浏览 0 评论 0 原文

如何以编程方式更新详细信息视图。不通过 SQL 数据源或通过向导。我想从代码隐藏 (.cs) 更新详细信息视图

我有一个加载了用户名的下拉列表,在选择用户名时,我正在加载包含用户信息的详细信息视图。

现在我想以编程方式提供更新功能。

我该怎么办?...

How can I programmatically Update Details View. NOT through SQL DataSource or through Wizard. I want to Update the Details View from Code- Behind (.cs)

I have a Drop down list loaded with UserNames, Up on Selecting the UserNAME, I am loading the Details View with User Information.

Now I want to Provide Update feature programmtically.

How can I do this ?..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2024-10-10 22:44:39

尝试在详细信息视图周围放置一个更新面板。向面板添加异步回发触发器 - 下拉列表的更改事件。

将下拉菜单设置为自动回发。

您无需完整回发即可获得所需的行为。

你也许可以做得更好,但这会起作用。

Try putting an Update Panel around the Details View. Add an async postback trigger to the panel - the change event of the drop down.

Set the dropdown to autopostback.

You'll get the desired behavior without a full postback.

You might be able to do better, but this will work.

眼睛会笑 2024-10-10 22:44:39

如果您在选择下拉列表后已经在任何表单上显示数据,那么可能
您正在使用“SelectedIndexChanged”事件。

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
      StringBuilder sb = new StringBuilder();
      // show user details for example by get the data from db
      string query = "SELECT userId, UserName FROM Users";
      SqlConnection conn = new SqlConnection("conn string");
      SqlCommand comd = new SqlCommand(query, conn);
      conn.Open();
      using(SqlDataReader r = comd.ExecuteReader())
      {
          while(r.Read()) 
          {                 
             sb.AppendLine(r.GetInt32(0) + ", " + r.GetString(1));                 
          }
          conn.Close();
      }
      textBox1.Text = sb.ToString();
 }

通过设置所需的索引来执行刷新数据:

this.comboBox1.SelectedIndex = 0;

它调用 SelectedIndexChanged 事件并刷新用户详细信息。

当然,你可以做得更有效率。

[编辑]

您可以通过在连接到数据库的 DataGridView 控件中显示详细视图来更新数据库中的数据:

SqlDataAdapter adapter = new SqlDataAdapter("select * from users", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

在 DataGridView 中编辑数据,然后:

adapter.Update(ds);

完成!

If you already displaying data on any form after drop down list selection, propably
you're using "SelectedIndexChanged" event.

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
      StringBuilder sb = new StringBuilder();
      // show user details for example by get the data from db
      string query = "SELECT userId, UserName FROM Users";
      SqlConnection conn = new SqlConnection("conn string");
      SqlCommand comd = new SqlCommand(query, conn);
      conn.Open();
      using(SqlDataReader r = comd.ExecuteReader())
      {
          while(r.Read()) 
          {                 
             sb.AppendLine(r.GetInt32(0) + ", " + r.GetString(1));                 
          }
          conn.Close();
      }
      textBox1.Text = sb.ToString();
 }

perform to refresh data by seting desired index:

this.comboBox1.SelectedIndex = 0;

it invokes SelectedIndexChanged envent and refresh user detailed information.

Of course you can do it more efficient.

[EDIT]

You can update data in database by displaying the detailed view in DataGridView control connected to db:

SqlDataAdapter adapter = new SqlDataAdapter("select * from users", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

edit data in DataGridView and after all:

adapter.Update(ds);

done!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文