【问题标题】:Data Binding Cells Of DataGridViewDataGridView的数据绑定单元格
【发布时间】:2011-06-08 15:40:39
【问题描述】:

我有一个函数可以对作为参数传递的DataGridViewRow 的一个单元格进行数据绑定。

public static void DataBindCells(DataGridViewRow row)
{
    DataGridViewComboBoxCell priceModes = row.Cells["ColumnPriceMode"] as DataGridViewComboBoxCell;
    priceModes.DataSource = UtilityClass.GetDataTable("SELECT PriceModeID,PriceModeName FROM PriceModes");
    priceModes.DisplayMember = "PriceModeName";
    priceModes.ValueMember = "PriceModeID";
}

这个功能的使用: 有一个调用DataBindCells 函数的“添加行”按钮。 使用该功能的DataGridView实际上是用来填写发票的。行的列是: ItemName、PriceMode、价格、数量、金额。

价格模式为 Piece/kg/Dozen 等。 当用户想要在账单中添加一个项目时,点击添加行按钮。

问题是在执行上述函数时,DataGridViewComboBoxCell 中没有任何选择。 有什么方法可以默认选择第一个项目。

【问题讨论】:

  • DataGrid 本身是数据绑定的吗?
  • 不,我正在使用该函数手动添加行。

标签: c# .net winforms datagridview


【解决方案1】:

下面的例子证明 DefaultValuesNeeded 可以做你想做的事。

...

命名空间 WindowsFormsApplication1 {

public static class Helper {
    public static DataTable ToDataTable<T>(this List<T> list) where T : class {
        Type type = typeof ( T );
        var ps = type.GetProperties ( );
        var cols = from p in ps
                   select new DataColumn ( p.Name , p.PropertyType );

        DataTable dt = new DataTable ( );
        dt.Columns.AddRange ( cols.ToArray ( ) );

        list.ForEach ( (l) => {
            List<object> objs = new List<object> ( );
            objs.AddRange ( ps.Select ( p => p.GetValue ( l , null ) ) );
            dt.Rows.Add ( objs.ToArray ( ) );
        } );

        return dt;
    }
}

public enum SendTypes {
    WeiBo ,
    QQ ,
    MSN ,
    EML
}

public class Receiver {
    public string Address {
        get;
        set;
    }
    public SendTypes SendType {
        get;
        set;
    }
    public string Msg {
        get;
        set;
    }
}

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent ( );
    }

    private void SetDataGrid() {
        DataGridViewComboBoxColumn colSendType = new DataGridViewComboBoxColumn ( );
        colSendType.Items.AddRange ( SendTypes.EML, SendTypes.MSN, SendTypes.QQ, SendTypes.WeiBo );
        colSendType.Name = "SendType";

        colSendType.DataPropertyName = "SendType";
        this.dataGridView1.Columns.Add ( colSendType );

        DataGridViewTextBoxColumn colAddress = new DataGridViewTextBoxColumn ( );
        colAddress.Name = "Address";
        colAddress.DataPropertyName = "Address";
        this.dataGridView1.Columns.Add ( colAddress );

        this.dataGridView1.AutoGenerateColumns = false;
        //this.dataGridView1.AllowUserToAddRows = true;
    }

    private void LoadData() {
        var tmp = new Receiver {
            Address = "http://www.weibo.com/?uid=1000",
            SendType = SendTypes.WeiBo,
            Msg = "Test"
        };
        List<Receiver> datas = new List<Receiver>();
        datas.Add(new Receiver {
            Address = "http://www.weibo.com/?uid=1000",
            SendType = SendTypes.WeiBo,
            Msg = "Test"
        });
        datas.Add(new Receiver(){
            Address = "10001",
            SendType = SendTypes.QQ,
            Msg = "test"
        });
        datas.Add(new Receiver(){
            Address = "xling@abc.com",
            SendType = SendTypes.EML,
            Msg = "TEST TEST"
        });

        this.dataGridView1.DataSource = datas;//.ToDataTable();
    }

    private void Form1_Load(object sender , EventArgs e) {
        this.SetDataGrid ( );
        this.LoadData ( );
    }

    private void dataGridView1_DefaultValuesNeeded(object sender , DataGridViewRowEventArgs e) {
        dataGridView1.Rows[e.Row.Index].Cells["SendType"].Value = SendTypes.EML;
    }
}

}

【讨论】:

    【解决方案2】:

    需要默认值

    Cell[2] 是 DataGridViewComboBoxColumn

        private void dataGridView1_DefaultValuesNeeded(object sender , DataGridViewRowEventArgs e) {
            dataGridView1.Rows[e.Row.Index].Cells[2].Value = "EML";
        }
    

    【讨论】:

    • 谢谢,但这将使 EML 成为选定的值。我想要第一个项目,不管它可能被默认选中。
    • 对不起,我不明白你的意思。 “有什么方法可以默认选择第一个项目。” ??如果要选择第一个,可以从 "SELECT PriceModeID,PriceModeName FROM PriceModes" 中获取第一个 "PriceModeID" ,然后将其设置为默认值。
    • 当我的意思是组合框单元包含多个项目(公斤/打/价格等)。物品数量未知。数据绑定时添加的第一个项目应该是默认选择的项目。如果第一项是 kg,则组合框应显示 kg 已选中。希望你明白了。您的答案始终选择 EML 作为选定值。
    【解决方案3】:

    在完全绑定 GridView 后添加选择条件(您希望选择哪一行)。一旦你控制并开始动态绑定,自动魔法的东西就会飞出窗口。

    你想在这里完成什么?我想有一种更简单的方法可以做到这一点,它更符合标准绑定模型和事件。

    【讨论】:

    • 我想我无法正确解释这个问题。请参阅我更新的问题。
    • 如果您是手动绑定元素,则需要以编程方式确定添加行的位置,然后以编程方式设置选定项。如果它始终是“最新添加的”,那么您可以在每次绑定时执行此操作,至少在理论上是这样(必须仔细检查代码才能确定)。如果没有,您必须弄清楚规则集,以便您可以正确添加“选定”。希望这会有所帮助
    • 抱歉,我对选择 ROW 不感兴趣。我有兴趣在 DataGridViewComboBoxCell.xling 的答案中选择第一项,这可能会让您更接近答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    相关资源
    最近更新 更多