更新数据库复选框 [英] updating database with checkboxes

查看:93
本文介绍了更新数据库复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid和每一行上有一个复选框。同时每一行的每一个领域可以更新

用户可以更新多行和检查任何复选框。

在提交按钮的所有数据应该被更新的点击。我需要的复选框来更新数据库中的布尔(或什么都bool类型的SQL Server)。


  1. 如何实现复选框,以便能够在数据库更新表?

  2. 的提交按​​钮的点击我如何获得的所有数据进行更新?


解决方案

通过DataGrid去和存储所有数据要在阵列或类似的更新。 SQL Server有一个数据类型,你可以,如果该复选框关闭或 0 > 1 如果是检查。一旦你收集的所有数据将它传递给你的数据层的SQL更新

您可以使用的DataGrid 对象通过其细胞/控件进行迭代。例如,使用一个嵌套循环你可以这样做:

  myDG.Items [索引1] .Cells [索引2] .Controls [0]

修改:这取决于输入控制你在列,因为你必须扮演他们。假设你有10列和文本框中的所有列DataGrid中除了最后一个,这是复选框,你会怎么做:

 复选框CB = NULL;
文本框TB = NULL;
清单<串GT; myList中=新名单<串GT;();
对于(INT行= 0;&行LT; myDG.Items.Count;排++)
{
  对于(INT COL = 0;&山坳下,myDG.Columns.Count;西++)
  {
    如果(COL&10 9){
      TB = myDG.Items [行] .Cells [COL] .Controls [0]为文本框;
      myList.Add(tb.Text);
    }
    其他{
      CB = myDG.Items [行] .Cells [COL] .Controls [0]为复选框;
      myList.Add(((cb.Checked)1:0));
    }
  }
}

您可以使用一个二维数组/列表保存,如果你想太多。 HTH

i have a datagrid and every row has a checkbox on it. also every field in every row can be updated

the user can update multiple rows and check any checkboxes.

at the click of the SUBMIT button all the data should be updated. i need the checkboxes to update a boolean (or what ever the bool type is for sql server) in a database.

  1. how do implement the checkboxes to be able to update the table in the DB?
  2. with the click of the submit button how do i get all the data to be updated?

解决方案

Go through the datagrid and store all of the data that you want to update in arrays or the like. Sql server has a bit datatype and you can set it to 0 if the checkbox is off or 1 if it is checked. Once you collect all the data pass it to your data layer for a sql update

You can use the DataGrid object to iterate through its cells/controls. For example, using a nested loop you can do:

myDG.Items[index1].Cells[index2].Controls[0]

Edit: It depends on the input controls you have in the columns because you have to cast them. Say you have a datagrid with 10 columns and TextBoxes in all columns except the last, which is CheckBox, you would do:

CheckBox cb = null;
TextBox tb = null;
List<string> myList = new List<string>();
for(int row = 0; row < myDG.Items.Count; row++)
{
  for(int col = 0; col < myDG.Columns.Count; col++)
  {
    if(col < 9){
      tb = myDG.Items[row].Cells[col].Controls[0] as TextBox;
      myList.Add(tb.Text);
    }
    else{
      cb = myDG.Items[row].Cells[col].Controls[0] as CheckBox;
      myList.Add(((cb.Checked) ? "1" : "0"));
    }
  }
}

You could use a 2D array/list to store if you wanted too. HTH

这篇关于更新数据库复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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