4

私が2つのテーブルを持っているとしましょう:

 work_hours
 work_hours_id | date  | _project_id
     1            1.2.      10
     2            1.2.      11
     3            1.2.      10 

 project
 project_id | project_name
    10           pr1
    11           pr2
    12           pr3

DataGridViewで私はこれを見たいです:

 work_hours_id | date | _person_id | project_name(DataGridViewComboBoxColumn)
     1            1.2.     10            pr1
     2            1.2.     11            pr2
     3            1.2.     10            pr1

1.どうすればそれができますか? 2.work_hours pr1(work_hours_id = 3)をpr3(DataGridViewComboBoxColumn)に変更した場合、変更をテーブルに保存することは可能SqlCommandBuilderですか?

 string query = "SELECT work_hours.work_hours_id, work_hours.date FROM work_hours
           LEFT OUTER JOIN project ON work_hours._project_id = project.project_id ORDER BY work_hours.date;
         SELECT * FROM project ORDER BY project_name";

            SqlCommand sqlcmd = new SqlCommand(query, conn);
            da = new SqlDataAdapter(query, connectionString);
            cBuilder = new SqlCommandBuilder(da);
            dt = new DataTable();
            ds = new DataSet();
            da.Fill(dt);
            da.Fill(ds);

            DataGridViewComboBoxColumn columnCb = new DataGridViewComboBoxColumn();
            columnCb.DataPropertyName = "_project_id";

            columnCb.DataSource = ds.Tables[1];
            columnCb.ValueMember = "project_id";
            columnCb.DisplayMember = "project_name";

            bSource = new BindingSource();
            bSource.DataSource = dt;
            dataGridView1.DataSource = bSource;
            dataGridView1.Columns.Add(columnCb);

次に例を示します。

4

1 に答える 1

2

誤解しない場合は、datagridviewのコンボボックス列のデータソースを設定する必要があります。これで設定できます。

/// if you use OfType Method then you will get advantage of to use as normal control
/// means this method will give you advantage of can use all method and properties
/// of the control which you want to implement in DataGridView

/// Controls.OfType method will check all the columns and get an array with 
/// what you give as search criteria..here, the criteria is ComboboxColumn..
/// depends of your need you can give comboboxcell also..

/// Element at method selects the zero based index in array which filtered by criteria
/// if you use only one of the given type then you can use .First() instead of .ElementAt()


yourDataGridViewName.Controls.OfType<DataGridViewComboBoxColumn>()
.ElementAt(indexNoOfTheDGVComboBox).DataSource = YourDataRetrievingMethod; 
//i.e. ds.Tables[indexNoOfTheTable].Columns[IndexOfTheColumn]

/// and you can set DisplayMember and ValueMember as the same way.. 
/// i give combocell and combocolumn together to show the syntax.

yourDataGridViewName.Controls.OfType<DataGridViewComboBoxCell>()
.ElementAt(0).DisplayMember = "YourDisplay";

yourDataGridViewName.Controls.OfType<DataGridViewComboBoxCell>()
.ElementAt(0).ValueMember = "YourValue"; 

2番目の質問について..あなたが私に尋ねるなら、私は好みます:「誤って節約する」を制御するには、行の最後にbuttonCellを置きます..ユーザーが行で何かを変更するときは、保存ボタンをクリックする必要があります..次に、ボタンをクリックします通常の挿入または更新メソッドとして変更を保存できるメソッド。

そこには2つの違いがあります。

1-)イベントを使用し、それが通常のイベントではなくメソッドCell_Clickによるボタンチェックであることを確認する必要がありますOfType()Button _Click

2-)プロジェクトのニーズに応じて、SQLQueryに値を取得して渡す前にRowIndex、DataGridViewを取得する必要があります。Cell Address()

于 2012-06-06T21:24:35.960 に答える