您的位置:首页 > 其它

DataGridViewComboBoxColumn 使用

2009-12-25 11:23 429 查看
用于编辑单元格时间时发生

private void dgvBreed_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
EditingControl = e.Control;

if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
ComboBox cmb = (ComboBox)e.Control;

if (cmb.Tag == null)
{
cmb.SelectedIndexChanged += new EventHandler(InformHandle_SelectedIndexChanged);
}

cmb.Tag = dgvBreed.CurrentRow.Index;
}
else if (dgvBreed.Columns[dgvBreed.CurrentCell.ColumnIndex].Name == "报价")
{
TextBox txt = (TextBox)e.Control;

txt.KeyDown += new KeyEventHandler(dgvBreed_KeyDown);
}

}

private void InformHandle_SelectedIndexChanged(object sender, EventArgs e)
{
if (!IsLockCombox)
{
DataGridViewRow row = ((DataGridView)((System.Windows.Forms.Control)sender).Parent.Parent).CurrentRow;
ComboBox cmb = (ComboBox)sender;

if (cmb.SelectedValue != null && int.Parse(cmb.Tag.ToString()) == dgvBreed.CurrentRow.Index)
{
string OldValue = dgvBreed.CurrentCell.Value.ToString();
string NewValue = cmb.SelectedValue.ToString();
if (OldValue != NewValue )
{
switch (NewValue)
{
#region 替代品种
case "替代品种":
{

BreedReplace breedreplace = new BreedReplace();

breedreplace.ShowDialog();

if (breedreplace.ReplaceID != -1)
{
DataTable dt = DataManage.getDataLib(DataLibtype.InformBreed);
//dt.Columns.Remove("库存");
DataRow[] dr = dt.Select("采购品种编号 = " + row.Cells["采购品种编号"].Value.ToString(), "", DataViewRowState.CurrentRows);
if (dr.Length > 0)
{
if (dt保存信息.Select("采购品种编号 = " + row.Cells["采购品种编号"].Value.ToString() + "and 库存 = '"+OldValue+"'", "", DataViewRowState.CurrentRows).Length <= 0)
dt保存信息.Rows.Add(dr[0].ItemArray);
}

row.Cells["中包报价"].Value = 0;
row.Cells["报价"].Value = 0;
row.Cells["数量"].Value = 0;
row.Cells["件数"].Value = 0;
row.Cells["中包数量"].Value = 0;

row.Cells["销售品种编号"].Value = breedreplace.ReplaceID;
row.Cells["销售品种"].Value = breedreplace.ReplaceName;
row.Cells["包装"].Value = breedreplace.S包装;
row.Cells["中包装"].Value = breedreplace.S中包装;
}
else
{
IsLockCombox = true;

for (int i = 0; i < cmb.Items.Count; i++)
{
if (cmb.Items[i].ToString().Equals(OldValue))
{
cmb.SelectedIndex = i;
break;
}
}

IsLockCombox = false;
}

break;
}
#endregion

#region 正常
case "正常":

if (OldValue == "替代品种")
{

DataTable dt = DataManage.getDataLib(DataLibtype.InformBreed);//.Rows.Add(dr[0].ItemArray);
DataRow[] dr = dt保存信息.Select("采购品种编号 = " + row.Cells["采购品种编号"].Value.ToString() + "and 库存 = '正常'", "", DataViewRowState.CurrentRows);
DataRow[] drr = dt.Select("采购品种编号 = " + row.Cells["采购品种编号"].Value.ToString(), "", DataViewRowState.CurrentRows);
if (dr.Length > 0)
{
drr[0]["采购数量"] = dr[0]["采购数量"];
drr[0]["采购件数"] = dr[0]["采购件数"];
drr[0]["采购中包数量"] = dr[0]["采购中包数量"];
drr[0]["数量"] = dr[0]["数量"];
drr[0]["件数"] = dr[0]["件数"];
drr[0]["中包数量"] = dr[0]["中包数量"];

drr[0]["报价"] = dr[0]["报价"];
drr[0]["中包报价"] = dr[0]["中包报价"];
}

drr[0]["销售品种编号"] = row.Cells["采购品种编号"].Value;
drr[0]["销售品种"] = 0;

}
else
{
DataTable dt = DataManage.getDataLib(DataLibtype.InformBreed);
DataRow[] drr = dt.Select("采购品种编号 = " + row.Cells["采购品种编号"].Value.ToString(), "", DataViewRowState.CurrentRows);
if (drr.Length > 0)
{
drr[0]["数量"] = drr[0]["采购数量"].ToString();
drr[0]["件数"] = drr[0]["采购件数"].ToString();
drr[0]["中包数量"] = drr[0]["采购中包数量"].ToString();

}
}

break;
#endregion

#region 不足
case "不足":

if (OldValue == "替代品种")
{
DataTable dt = DataManage.getDataLib(DataLibtype.InformBreed);
DataRow[] dr = dt保存信息.Select("采购品种编号 = " + row.Cells["采购品种编号"].Value + "and 库存 = '不足'", "", DataViewRowState.CurrentRows);
DataRow[] drr = dt.Select("采购品种编号 = " + row.Cells["采购品种编号"].Value.ToString(), "", DataViewRowState.CurrentRows);

if (dr.Length > 0)
{
drr[0]["采购数量"] = dr[0]["采购数量"];
drr[0]["采购件数"] = dr[0]["采购件数"];
drr[0]["采购中包数量"] = dr[0]["采购中包数量"];
drr[0]["数量"] = dr[0]["数量"];
drr[0]["件数"] = dr[0]["件数"];
drr[0]["中包数量"] = dr[0]["中包数量"];

drr[0]["报价"] = dr[0]["报价"];
drr[0]["中包报价"] = dr[0]["中包报价"];
}

row.Cells["销售品种编号"].Value = row.Cells["采购品种编号"].Value;
row.Cells["销售品种"].Value = "";

}
row.Cells["数量"].Value = row.Cells["采购数量"].Value.ToString();
row.Cells["件数"].Value = row.Cells["采购件数"].Value.ToString();
break;

case "断货":
row.Cells["数量"].Value = 0;
row.Cells["件数"].Value = 0;
row.Cells["中包数量"].Value = 0;
break;
case "不经营":
row.Cells["数量"].Value = 0;
row.Cells["件数"].Value = 0;
row.Cells["中包数量"].Value = 0;
break;

#endregion

default:

break;
}
}
}

dgvBreed.Refresh();
}
}

http://hi.baidu.com/loveliulin09/blog/item/291504ca57178116bf09e658.html

http://topic.csdn.net/u/20070514/14/611b191f-eff4-423f-9c9b-29977f26482f.html

http://blog.csdn.net/freeliver54/archive/2009/10/27/4735848.aspx

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Sys
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AutoGenerateColumns = false;

using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=sa;database=xx"))
{
string sql1 = @"select userid,fullname,a.[description],b.ugroupid
from users a left outer join usergroup b
on a.grade=b.ugroupid";
string sql2 = "select distinct ugroupid,[description] from usergroup";

DataGridViewComboBoxColumn dgvComboBoxColumn = dataGridView1.Columns["grade"] as DataGridViewComboBoxColumn;
dgvComboBoxColumn.DataPropertyName = "ugroupid";
dgvComboBoxColumn.DataSource = GetTable(sql2).DefaultView;//必须在设置dataGridView1的DataSource的属性前设置
dgvComboBoxColumn.DisplayMember = "description";
dgvComboBoxColumn.ValueMember = "ugroupid";

dataGridView1.DataSource = GetTable(sql1).DefaultView;//一定要在dgvComboBoxColumn的DataSource后设置
}
}

private DataTable GetTable(string sql)
{
using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=yuling1310;database=smls"))
{
DataTable dt = new DataTable();
SqlDataAdapter sqlda = new SqlDataAdapter(sql, sqlconn);
sqlda.Fill(dt);
return dt;
}
}
}
}

注意事项:
.注意其ValueMember的DataType与DataPropertyName对应的列的DataType要相同,他不会为你做类型转换的。
2.编程设置显示样式时注意一定要在设置DataSource之前设置DataGridViewComboBoxColumn的DataSource等属性。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/freeliver54/archive/2009/10/27/4735848.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: