0

.NET 3.5 で Winforms アプリケーションを変更しています。

以下に示すように、いくつかのハードコードされたオプションが入力された DataGridViewComboBoxColumn があります。

// 
// dgvCol_PropName
// 
this.dgvCol_PropName.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
this.dgvCol_PropName.HeaderText = "Property Name";
this.dgvCol_PropName.Items.AddRange(new object[] {
"Option1",
"Option2",
"Option3"});
this.dgvCol_PropName.Name = "dgvCol_PropName";
this.dgvCol_PropName.Width = 150;

これらのオプションの選択を区別して、オプションがグリッドに存在すると、再度選択できないようにしたいと思います (ユーザーは現在の行を編集するか、削除して再入力する必要があります)。

これを行う簡単な方法はありますか?

他の人に役立つ場合に備えて、最終的な解決策を共有すると思いました。私の CellValidating イベントハンドラーは以下の通りです:

/// <summary>
/// Handle the validation event on the cell so that only properties that have not already been specified can be selected
/// </summary>
private void dg_FTPS_ExProps_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    var dataGrid = sender as DataGridView;
    if (dataGrid != null)
    {
        // Only validate if that event is generated for the Property Name column
        string headerText = dataGrid.Columns[e.ColumnIndex].HeaderText;

        // Abort validation if cell is not in the PropertyName column.
        // Rejected using the column index in case the column order changes but
        // equally, there will be problems if the column header changes. (Pay your money and take a chance)
        if (headerText.Equals("Property Name"))
        // if (e.ColumnIndex == 0) 
        {
            // Count the number of times the property exists in the table
            int propertyCount = 0;
            foreach (DataGridViewRow row in dg_FTPS_ExProps.Rows)
            {
                if( row.Cells[ e.ColumnIndex ].EditedFormattedValue.ToString().Equals( e.FormattedValue) == true )
                {
                    propertyCount++;
                }
            }

            // Check if multiple entreies have tried to be applied
            if (propertyCount > 1)
            {
                e.Cancel = true;
            }
        }
    }
}

インスタンスの数をカウントするために LINQ を使用したいと思っていましたが、まだそれを学んでいるので、私が知っていることに固執しました。

4

1 に答える 1

0

イベントの本文でこのイベントDataGridView.CellValidatingを使用してみてください。同じ値の別の行があるかどうか、およびに設定さe.Cancelれているかどうかを確認してくださいtrue

于 2011-09-02T13:29:57.587 に答える