我有一个函数,可以对作为参数传递的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,价格,数量,数量.
价格模式是件/公斤/打等.
当用户想要在账单中添加项目时,单击添加行按钮.
问题是执行上述功能时,在DataGridViewComboBoxCell中什么也没有选择.
有什么方法可以让默认情况下的第一个项目被选中.
解决方法:
下面的示例证明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;
}
}
}