基于数据表绑定每行中不同数据的radiogroup

时间:2017-12-20 23:23:24

标签: c# winforms devexpress

我添加了两个列的gridcontrol,第1列表示字符串并从datatable绑定,另一列表示radiogroupitem但我想在每行中添加不同的radiobutton值,基于第一行添加的问题 (罚款 - 坏)和下一行添加(此处 - 不在此处)和最后一行添加(尚未 - 是 - 今晚)

//bind question column
DataTable dtt = new DataTable();

dtt.Columns.Add("ID", typeof(string));

dtt.Rows.Add("How are you ?");
dtt.Rows.Add("Where are you ?");
dtt.Rows.Add("are you sleepy ?");

gridControl1.DataSource = dtt;

gridControl1.ForceInitialize();

// Bind radiobuttonitem
DataTable dataSource = new DataTable();
dataSource.Columns.Add("TypeID", typeof(int));
dataSource.Columns.Add("TypeName", typeof(string));

dataSource.Rows.Add(new object[] { 1, "A" });
dataSource.Rows.Add(new object[] { 2, "B" });
dataSource.Rows.Add(new object[] { 3, "C" });

foreach (DataRow dr in dataSource.Rows)
   repositoryItemRadioGroup1.Items
        .Add(new DevExpress.XtraEditors.Controls.RadioGroupItem(dr["TypeID"], dr["TypeName"].ToString()));

https://imgur.com/a/25Ofu

1 个答案:

答案 0 :(得分:1)

使用CustomRowCellEdit事件为不同的单元格分配不同的编辑器。您可以将问题和RepositoryItemRadioGroups存储在字典中:

Dictionary<string, RepositoryItemRadioGroup> repositories = new Dictionary<string, RepositoryItemRadioGroup>();

RepositoryItemRadioGroup group1 = new RepositoryItemRadioGroup();
group1.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem("Fine", "Fine"));
group1.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem("Bad", "Bad"));
repositories.Add("How are you?", group1);

RepositoryItemRadioGroup group2 = new RepositoryItemRadioGroup();
group2.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem("Here", "Here"));
group2.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem("There", "There"));
repositories.Add("Where are you?", group2);

在CustomRowCellEdit事件处理程序中,调用GetRowCellValue方法来获取问题,从字典中获取相应的存储库项并设置e.RepositoryItem参数:

void GridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) {
    GridView view = sender as GridView;
    if (e.Column.FieldName == "Answer" && view.IsValidRowHandle(e.RowHandle)) {
        string question = (string)view.GetRowCellValue(e.RowHandle, "Question");
        RepositoryItemRadioGroup item;
        if(repositories.TryGetValue(question, out item))
            e.RepositoryItem = item;
    }
}

另请参阅:Modify and Validate Cell Values