3

I have a function that databinds one of the cells of the DataGridViewRow passed as 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";
}

The use of this function: There is an "Add Row" button which calls the DataBindCells function. The DataGridView which uses this function is actually used for filling an invoice. The columns of row are: ItemName, PriceMode, Price, Quantity, Amount.

The price modes are Piece/kg/Dozen etc. When the user wants to add an item in the bill, the Add Row button is clicked.

The problem is that when the above function is executed, nothing is selected in the DataGridViewComboBoxCell. Is there any way to get the first item selected by default.

Akshay J
  • 5,362
  • 13
  • 68
  • 105

3 Answers3

2

The following example proved DefaultValuesNeeded can do what you want.

...

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

}

xling
  • 252
  • 1
  • 9
1

DefaultValuesNeeded

Cell[2] is DataGridViewComboBoxColumn

    private void dataGridView1_DefaultValuesNeeded(object sender , DataGridViewRowEventArgs e) {
        dataGridView1.Rows[e.Row.Index].Cells[2].Value = "EML";
    }
xling
  • 252
  • 1
  • 9
  • Thanks but that will make EML the selected value. I want the first item, whatever it maybe to get selected by default. – Akshay J Jun 08 '11 at 16:07
  • Sorry, I can't understand what you mean. "Is there any way to get the first item selected by default." ?? If you want select the first , you can get the first "PriceModeID" from "SELECT PriceModeID,PriceModeName FROM PriceModes" , then set it as default value. – xling Jun 08 '11 at 16:37
  • When I meant was that the comboboxcell contains multiple items(kg/dozen/price etc). The no of items is not known. The first item added while data-binding should be the item selected by default. If the first item is kg then the combobox should show kg as selected. Hope you got it. Your answer always selects EML as the selectedvalue. – Akshay J Jun 08 '11 at 16:47
0

Add the selection criteria (what row you would like selected) after fully binding the GridView. The automagic stuff flies out the window once you take control and start dynamically binding.

What are you trying to accomplish here? I would imagine there is an easier way to do this that is more inline with the standard binding model and events.

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32
  • I think I could not explain the question properly. See my updated question. – Akshay J Jun 08 '11 at 16:02
  • If you are manually binding elements, you need to determine location of the added row programatically and then programatically set the selected item. If it is always "latest added", you can do this on every bind, at least theoretically (have to examine the code closer to be sure). If not, you have to figure out the rule set so you can add "selected" properly. Hope this helps – Gregory A Beamer Jun 08 '11 at 16:18
  • Sorry but I am not interested in selecting ROW. I am interested in selecting the first item in a DataGridViewComboBoxCell.xling's answer may bring you closer to the answer. – Akshay J Jun 08 '11 at 16:37