我可以使用隐藏的DataGridView列来管理SQL Server VarBinary字段吗?

时间:2016-01-14 17:25:49

标签: c# sql-server winforms datagridview

我有一个表,我想在字段中存储一个字节数组。字节数组大约是20字节(160位)的密钥数据。

我正在使用多个DataGridView来管理此表以及此应用程序的其他表。我目前有几个例程允许我提供SQL选择字符串,DataGridView允许用户编辑数据。

private void InitializeUsersDataGrid()
{
  string sql = "SELECT UserId, Enabled, AccessLevel, Name, KeyValue FROM Users";

  DataGridViewIntialize(dgvUsers, sql);
  dgvUsers.Columns[fdKeyValue].Visible = false;
}

private void DataGridViewIntialize(DataGridView dataGridView, string sql)
{
  dataGridViewInUse = dataGridView; // This is the current DataGridView
  OleDbConnection oleDbConnection = new OleDbConnection(txtConnectionString.Text);
  dataAdapter = new OleDbDataAdapter(sql, oleDbConnection);
  OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);  //Creates SQL commands for IUD

  dataTable = new DataTable();
  dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;

  dataAdapter.Fill(dataTable);
  bindingSource1.DataSource = dataTable;

  dataGridView.DataSource = bindingSource1;

  // Resize the DataGridView columns to fit the newly loaded content.
  dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
  AddDeleteRefreshContextMenu(dataGridView);
}

我有一个提供验证的保存按钮,然后调用TableSave

private void TableSave()
{
  // Update the database with the user's changes.
  try {
    dataAdapter.Update((DataTable)bindingSource1.DataSource);
  }
  catch (Exception ex) {
    MessageBox.Show(ex.Message);
  }
}

当我将varbinary字段添加到表格时,它在DataGridView中显示为损坏的位图图像。这并不关心我,因为无论如何我都不打算显示它。

当我尝试为调用分配一个新的字节数组时,它会生成一个DataGridView默认错误对话框。

/// <summary>
/// Change the password hash for the selected login
/// </summary>
/// <param name="e"></param>
void ChangePassword( DataGridViewCellEventArgs e)
{
  // Request a new password with confirmation
  fPassword form = new fPassword();
  form.Confirm = true;
  DialogResult dr = form.ShowDialog();
  if (dr == DialogResult.OK) {
    // Combine the UserID with the password to generate a new key
    byte[] salt;
    byte[] key;
    string p = dgvUsers.Rows[e.RowIndex].Cells["UserId"].Value + form.Value;
    Data.PBK2DF2Hash.GenerateSaltAndKey(p, out salt, out key);
    if (dgvUsers.Rows[e.RowIndex].Cells[fdKeyValue].ValueType == key.GetType()) {
      // We do get this far Error occurs on the assignment in the next line
      dgvUsers.Rows[e.RowIndex].Cells[fdKeyValue].Value = key;
    } 
  }
}
  

DataGridView默认错误对话框

     

DataGridView中发生以下异常:System.ArgumentException:参数无效   在System.Drawing.Image.FromStream(Stream stream,Boolean   useEmbeddedColorManagement,Boolean validateImageData)    在   System.Drawing.ImageConverter.ConvertFrom(ITypeDescriptorContext   语境,文化信息文化,对象价值)    在   System.Windows.Forms.Formatter.FormatObjectInternal(Object value,Type   targetType,TypeConverter sourceConverter,TypeConverter   targetConverter,String formatString,IFormatProvider formatInfo,   对象formattedNullValue)    在   System.Windows.Forms.Formatter.FormatObject(Object value,Type   targetType,TypeConverter sourceConverter,TypeConverter   targetConverter,String formatString,IFormatProvider formatInfo,   Object formattedNullValue,Object dataSourceNullValue)    在   System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value,   Int32 rowIndex,DataGridViewCellStyle&amp; cellStyle,TypeConverter   valueTypeConverter,TypeConverter formattedValueTypeConverter,   DataGridViewDataErrorContexts context)

     

要替换此默认对话框,请处理DataError事件。

它似乎与CellFormatting有关,但我不知道如何将其关闭。

添加此代码可以使用此错误消息提供更简单的对话框:

  

System.FormatException:单元格的格式化值类型错误。

private void dgvUsers_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  DataGridViewColumn c = dgvUsers.Columns[fnKeyValue];
  if (c != null) {
    if (e.ColumnIndex == c.Index) {
      if (e.Value != null) {
        e.FormattingApplied = true;
      }
      else
        e.FormattingApplied = false;
    }
  }
}

更新#1 - 使用Ivan Stoev推荐方法的附加代码,并显示我用来请求更改密码的按钮单元格。它还增加了节省盐的价值。

此时的问题是,当我点击“更改密码”按钮时,我可以打开密码对话框,获取密码。生成salt和key,将其保存到该行/ s salt和keyValue的datagrid单元格中。

如果我执行TableSave()(请参阅前面的代码),除非我在网格中的另一行上单击鼠标,否则这些字段不在SQL服务器表中。该操作似乎表明该行是脏的,然后保存将起作用。

我也可以在更改密码之前或之后更改用户可见的其中一个字段,该行将保存。

private void InitializeUsersDataGrid()
{
  string sql = "SELECT UserId, Enabled, AccessLevel, Name, Salt, KeyValue FROM Users";

  AddAccessLevelColumn(dgvUsers);  //Add column if needed
  AddPasswordChangeColumn(dgvUsers);
  DataGridViewIntialize1(dgvUsers, sql);
  dgvUsers.Columns[UsersFieldName.fdAccessLevel].Visible = false;  //Hide the raw column from the database

  MoveAccessLevelColumn(dgvUsers, AccessLevelComboBoxColumn, AccessLevelColumnPosition);
  MoveAccessLevelColumn(dgvUsers, PasswordButtonColumn, PasswordColumnPosition);
}

private void DataGridViewIntialize1(DataGridView dataGridView, string sql)
{
  dataGridViewInUse = dataGridView; // This is the current DataGridView
  OleDbConnection oleDbConnection = new OleDbConnection(txtConnectionString.Text);
  dataAdapter = new OleDbDataAdapter(sql, oleDbConnection);
  OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);  //Creates SQL commands for IUD

  dataTable = new DataTable();
  dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;

  dataAdapter.Fill(dataTable);
  dataTable.Columns[Data.UsersFieldName.fdSalt].ColumnMapping = MappingType.Hidden;
  dataTable.Columns[Data.UsersFieldName.fdKey].ColumnMapping = MappingType.Hidden;
  bindingSource1.DataSource = dataTable;

  dataGridView.DataSource = bindingSource1;

  // Resize the DataGridView columns to fit the newly loaded content.
  dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
  AddDeleteRefreshContextMenu(dataGridView);
}

private void AddPasswordChangeColumn(DataGridView dataGridView)
{
  // This column will remain unless manually removed, so it only needs to be added
  // the first time the DVG is initialized.

  if (dataGridView.Columns[PasswordButtonColumn] == null) {
    // creating new ComboBoxCell Column
    DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();
    btnColumn.Text = "Change Password";
    btnColumn.HeaderText = "Change Password";
    btnColumn.UseColumnTextForButtonValue = true;
    btnColumn.Name = PasswordButtonColumn;
    btnColumn.FlatStyle = FlatStyle.Popup;
    dataGridView.Columns.Insert(0, btnColumn);
    // Add a CellClick handler to handle clicks in the button column.
    dataGridView.CellClick += dataGridView_CellClick;
  }
}

private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
  // Ignore clicks that are not on button cells.
  if (e.RowIndex >= 0 && e.RowIndex < dgvUsers.RowCount - 1 && e.ColumnIndex == dgvUsers.Columns[PasswordButtonColumn].Index) {
    ChangePassword(e);
  }
}

/// <summary>
/// Change the password hash for the selected login
/// </summary>
/// <param name="e"></param>
private void ChangePassword(DataGridViewCellEventArgs e)
{
  // Request a new password with confirmation
  fPassword form = new fPassword();
  form.Confirm = true;
  DialogResult dr = form.ShowDialog();
  if (dr == DialogResult.OK) {
    // Combine the UserID with the password to generate a new key
    byte[] salt;
    byte[] key;
    string p = dgvUsers.Rows[e.RowIndex].Cells[Data.UsersFieldName.fdUserId].Value + form.Value;
    Data.PBK2DF2Hash.GenerateSaltAndKey(p, out salt, out key);
    var gridRow = dgvUsers.Rows[e.RowIndex];
    var dataRow = (DataRowView)gridRow.DataBoundItem;
    var value = dataRow[Data.UsersFieldName.fdKey];
    dataRow[Data.UsersFieldName.fdSalt] = salt;  //This assignment now works
    dataRow[Data.UsersFieldName.fdKey] = key;
  }
}

更新#2 添加&#39; NotifyCurrectCell&#39;肮脏的改变密码&#39;方法通知网格需要保存单元格而不更改UI中的行。

private void ChangePassword(DataGridViewCellEventArgs e)
{
  // Request a new password with confirmation
  fPassword form = new fPassword();
  form.Confirm = true;
  DialogResult dr = form.ShowDialog();
  if (dr == DialogResult.OK) {
    // Combine the UserID with the password to generate a new key
    byte[] salt;
    byte[] key;
    string p = dgvUsers.Rows[e.RowIndex].Cells[Data.UsersFieldName.fdUserId].Value + form.Value;
    Data.PBK2DF2Hash.GenerateSaltAndKey(p, out salt, out key);
    var gridRow = dgvUsers.Rows[e.RowIndex];
    var dataRow = (DataRowView)gridRow.DataBoundItem;
    var value = dataRow[Data.UsersFieldName.fdKey];
    dataRow[Data.UsersFieldName.fdSalt] = salt;  //This assignment now works
    dataRow[Data.UsersFieldName.fdKey] = key;
    dgvUsers.NotifyCurrentCellDirty(true); //Tells the UI that this row has changed and needs updating
  }
}

1 个答案:

答案 0 :(得分:1)

您遇到的问题是因为DataGridView默认为byte[]数据类型创建了DataGridViewImageColumn

以下是一些选项。

  • 一个。将DataGridView.AutoGenerateColumns属性设置为false并手动创建网格列。

  • B中。如果您确实不需要网格中的该列,而是尝试隐藏网格列,请不要创建网格列。但是如何实现呢?对于类属性,可以使用Browsable(false),但这是DataTable。好吧,虽然没有记录,但可以将DataColumn.ColumnMapping属性与MappingType.Hidden用于同一目的。

在您的情况下,请删除此行

dgvUsers.Columns[fdKeyValue].Visible = false;

并在DataGridViewIntialize方法内使用

// ...
dataAdapter.Fill(dataTable);
dataTable.Columns[fdKeyValue].ColumnMapping = MappingType.Hidden;
bindingSource1.DataSource = dataTable;
// ...

您仍然可以使用此基础数据源

来访问您的列数据
var gridRow = dgvUsers.Rows[...];
var dataRow = (DataRowView)gridRow.DataBoundItem;
var value = dataRow[fdKeyValue];