updating database with checkboxes

前端 未结 2 2063
礼貌的吻别
礼貌的吻别 2021-01-23 12:31

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

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-23 12:42

    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 myList = new List();
    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

提交回复
热议问题