DataGridView Cell Binding Data

I have a function that binds one of the cells DataGridViewRowpassed as an argument.

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";
}

Usage of this function: There is a "Add line" button that calls the function DataBindCells. DataGridViewthat uses this function is actually used to fill out an invoice. Columns of the row: ItemName, PriceMode, Price, Quantity, Amount.

Price modes - Piece / kg / Dozen, etc. When the user wants to add an item to the account, click the "Add line" button.

The problem is that when you execute the above function, DataGridViewComboBoxCellnothing is selected. Is there a way to get the first item selected by default.

+3
source share
3

, 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

DefaultValuesNeeded

[2] - DataGridViewComboBoxColumn

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

Add selection criteria (which row you want to select) after the GridView is fully linked. Automatic material flies out of the window as soon as you take control and begin to dynamically bind.

What are you trying to do here? I would suggest that there is an easier way to do this, which is more consistent with the standard binding model and events.

0
source

All Articles