我有一个函数可以对作为参数传递的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,Price,Quantity,Amount。
价格模式是件/公斤/打等。 当用户想要在帐单中添加项目时,单击“添加行”按钮。
问题是当执行上述功能时,DataGridViewComboBoxCell
中没有选择任何内容。
有没有办法在默认情况下选择第一个项目。
答案 0 :(得分:2)
以下示例证明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;
}
}
}
答案 1 :(得分:1)
DefaultValuesNeeded
Cell [2]是DataGridViewComboBoxColumn
private void dataGridView1_DefaultValuesNeeded(object sender , DataGridViewRowEventArgs e) {
dataGridView1.Rows[e.Row.Index].Cells[2].Value = "EML";
}
答案 2 :(得分:0)
在完全绑定GridView之后添加选择条件(您希望选择哪一行)。一旦你掌控并开始动态绑定,自动化的东西就会飞出窗外。
你想在这里完成什么?我想象有一种更简单的方法可以做到更符合标准的绑定模型和事件。