public partial class FrmFieldMapping : DevComponents.DotNetBar.Office2007Form
{
private AMDataFieldCollection linkEntryFields; string linkCategoryId;
AMCategory[] categorys;
/// <summary>
/// 当前的分类
/// </summary>
string currCategoryId;
///// <summary>
///// 数据库里面读取过来的集合
///// </summary>
List<AMLinkFieldItem> LstLinkDataGridViewFieldItem = new List<AMLinkFieldItem>();
/// <summary>
/// 存储序列化信息的类
/// </summary>
AMBusinessLinkArchival currLink = new AMBusinessLinkArchival();
public FrmFieldMapping()
{
if (currLink == null)
{
currLink.LinkCategoryId = string.Empty;
currLink.LstLinkFieldItem = new List<AMLinkFieldItem>();
}
InitializeComponent();
this.dataGridView.AllowUserToResizeRows = false;
//防止出现 datagridviewcomboboxcell 值无效 这个错误
this.dataGridView.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e) { };
//控件事件
this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
this.cbxCategory.SelectedIndexChanged += new EventHandler(cbxCategory_SelectedIndexChanged);
this.dataGridView.CellEnter += dataGridView_CellEnter; //下拉框数据
this.categorys = ApplicationEx.ClientInterface.ListCategories();
List<ComboxItem> lstComboItem = new List<ComboxItem>();
for (int i = ; i < this.categorys.Length; i++)
{
ComboxItem item = new ComboxItem();
item.ItemText = this.categorys[i].CategoryId + "." + this.categorys[i].Name;
item.ItemValue = this.categorys[i].CategoryId;
lstComboItem.Add(item);
} this.cbxCategory.Items.Clear();
this.cbxCategory.Items.AddRange(lstComboItem.ToArray());
this.cbxCategory.DisplayMember = "ItemText";
this.cbxCategory.ValueMember = "ItemValue"; string[,] arryFields = new string[,]
{
{ "string", "DJH", "地籍号" },
{ "string", "TDQLR", "土地权利人" },
{ "string", "ZJMC", "证件名称" },
{ "string", "ZJHM", "证件号码" },
{ "string", "ZSH", "土地证号" },
{ "string", "ZDH", "宗地号" },
{ "string", "DJFZSJ", "登记发证时间" },
{ "string", "SYQMJ", "使用权面积"},
{ "string", "QSXZ", "权利性质" },
{ "string", "SYQLX", "使用权类型"},
{ "string", "SYQX", "使用期限" },
{ "string", "TDYT", "土地用途" },
{ "string", "TDDJ", "土地等级" },
{ "string", "JZZDMJ", "建筑占地面积" },
{ "string", "DZSYMJ", "独自使用面积"},
{ "string", "QTSX", "其他事项"},
{ "string", "BZ", "备注" },
{ "string", "TDZL", "土地坐落"},
{ "string", "TDMJ", "土地面积" },
}; for (int k = ; k < arryFields.GetLength(); k++)
{
AMLinkFieldItem fieldItem = new AMLinkFieldItem();
for (int j = ; j < arryFields.GetLength(); j++)
{
if (j == )
{
fieldItem.BusinessFiledDateType = arryFields[k, j];
}
else if (j == )
{
fieldItem.BusinessFiledName = arryFields[k, j];
}
else if (j == )
{
fieldItem.BusinessFiledCaption = arryFields[k, j];
}
}
LstLinkDataGridViewFieldItem.Add(fieldItem);
} //前两列数据绑定
foreach (AMLinkFieldItem field in LstLinkDataGridViewFieldItem)
{
DataGridViewRow dgvRow = this.dataGridView.Rows[this.dataGridView.Rows.Add()];
dgvRow.Cells[DateType.Index].Value = field.BusinessFiledDateType;
dgvRow.Cells[currFieldName.Index].Value = field.BusinessFiledName;
dgvRow.Cells[currCategoryField.Index].Value = field.BusinessFiledCaption;
//事先设定好行的tag
dgvRow.Tag = field;
} //从数据库加载列表集合
string queryKey = string.Format("FrmFieldMapping").ToUpper();
string favoritesKeyValue = ApplicationEx.ConnectionInterface.GetPropertyValue(queryKey);
currLink = ApplicationEx.DeserializeToObject(Convert.FromBase64String(favoritesKeyValue)) as AMBusinessLinkArchival; if(currLink!=null&&currLink.LinkCategoryId!=null)
{
//绑定类别的下拉表
for (int i = ; i < cbxCategory.Items.Count; i++)
{
if ((cbxCategory.Items[i] as ComboxItem).ItemValue.ToString() == currLink.LinkCategoryId)
{
cbxCategory.SelectedItem = cbxCategory.Items[i];
break;
}
}
}
} void dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
//实现单击一次显示下拉列表框
if (dataGridView.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && e.RowIndex != -)
{
SendKeys.Send("{F4}");
}
} /// <summary>
/// 点击类别列表的时候切换关联信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void cbxCategory_SelectedIndexChanged(object sender, EventArgs e)
{
this.linkCategoryId = ((this.cbxCategory.SelectedItem as ComboxItem).ItemValue as string);
this.linkEntryFields = ApplicationEx.ClientInterface.ListDataFields(this.linkCategoryId); BuildMapFieldListCBX(); this.dataGridView.Focus();
currCategoryId = linkCategoryId;
//如果数据库有信息就加载数据库的
if (currLink!=null&&currLink.LinkCategoryId == currCategoryId)
{
foreach (DataGridViewRow dgvRow in this.dataGridView.Rows)
{
foreach (AMLinkFieldItem comboxField in currLink.LstLinkFieldItem)
{
if (comboxField.BusinessFiledName == dgvRow.Cells[currFieldName.Index].Value.ToString())
{
if (this.linkEntryFields.Contains(comboxField.LinkArchivalFiledName))
{
dgvRow.Cells[cmbLinkField.Index].Value = this.linkEntryFields[comboxField.LinkArchivalFiledName];
}
break;
}
}
}
}
else
{
//如果数据库没有信息就加载默认的匹配讯息
foreach (DataGridViewRow dgvRow in this.dataGridView.Rows)
{
//遍历行然后转换每一行的tag成为AMLinkFieldItem类
AMLinkFieldItem linkItem = dgvRow.Tag as AMLinkFieldItem;
/*然后循环AMDataField找到和AMLinkFieldItem里面的caption显示名称一样的类,然后再赋值给当前行的下拉表的默认值,如果没有的话就制空该行的下拉框
*保证把每行的下拉列表的默认值做一个初始化设置默认值的操作,
*关键字:下拉列表绑定类:AMDataField dagairidview绑定类:AMLinkFieldItem 比较对象:caption
*/
foreach (AMDataField field in this.linkEntryFields)
{
if (field.Caption == linkItem.BusinessFiledCaption)
{
dgvRow.Cells[this.cmbLinkField.Index].Value = field;
break;
}
else
{
dgvRow.Cells[this.cmbLinkField.Index].Value = null;
}
}
} }
} /// <summary>
/// 构造gridview下拉框的数据
/// </summary>
void BuildMapFieldListCBX()
{
Dictionary<string, string> dicComboxItems = new Dictionary<string, string>();
List<ComboxItem> test = new List<ComboxItem>(); cmbLinkField.Items.Clear();
List<ComboxItem> lstComboItem = new List<ComboxItem>();
ComboxItem item = new ComboxItem();
//空格选项
item.ItemText ="";
item.ItemValue = "";
lstComboItem.Add(item);
for (int i = ; i < this.linkEntryFields.Count; i++)
{
item = new ComboxItem();
item.ItemText = this.linkEntryFields[i].Caption;
item.ItemValue = this.linkEntryFields[i];
lstComboItem.Add(item);
}
cmbLinkField.Items.AddRange(lstComboItem.ToArray());
cmbLinkField.DisplayMember = "ItemText";
cmbLinkField.ValueMember = "ItemValue";
} public class ComboxItem
{
private string itemText; public string ItemText
{
get { return itemText; }
set { itemText = value; }
}
private Object itemValue; public Object ItemValue
{
get { return itemValue; }
set { itemValue = value; }
}
public override string ToString()
{
return this.ItemText;
} }
/// <summary>
/// 建立关联信息并且放入数据库
/// </summary>
void BuildLinkCondition()
{
try
{
//初始化集合
if(currLink!=null)
{
currLink.LstLinkFieldItem.Clear();
}
int index = ;
List<AMLinkFieldItem> LstLinkFieldItem = new List<AMLinkFieldItem>();
foreach (DataGridViewRow dgvRow in this.dataGridView.Rows)
{
if (!string.IsNullOrEmpty(dgvRow.Cells[DateType.Index].Value.ToString()))
{
//加载的时候自动匹配的数据
if (dgvRow.Cells[cmbLinkField.Index].Value != null)
{
AMLinkFieldItem fieldItems = new AMLinkFieldItem();
fieldItems.BusinessFiledDateType = dgvRow.Cells[DateType.Index].Value.ToString();
fieldItems.BusinessFiledName = dgvRow.Cells[currFieldName.Index].Value.ToString();
fieldItems.BusinessFiledCaption = dgvRow.Cells[currCategoryField.Index].Value.ToString();
fieldItems.LinkArchivalFiledName = (dgvRow.Cells[cmbLinkField.Index].Value as AMDataField).Name;
LstLinkFieldItem.Add(fieldItems);
}
}
index++;
}
//构造信息的类并且加入数据库
AMBusinessLinkArchival currSaveLink = new AMBusinessLinkArchival();
currSaveLink.LinkCategoryId = currCategoryId;
currSaveLink.LstLinkFieldItem = LstLinkFieldItem;
//序列化并且调用加入数据库的方法存入属性表
string queryKey = string.Format("FrmFieldMapping").ToUpper();
string queryKeyValue = string.Empty;
queryKeyValue = Convert.ToBase64String(ApplicationEx.SerializerToByte(currSaveLink));
ApplicationEx.ConnectionInterface.SetPropertyValue(queryKey, queryKeyValue);
//最后关闭窗体
this.Close();
}
catch (Exception ex)
{
ApplicationEx.ShowErrorMessageBox(this, ex);
}
} private void btnOk_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.linkCategoryId))
{
ApplicationEx.ShowMessageBox(this, "关联分类为空,请选择!");
return;
}
BuildLinkCondition();
} private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
//清空记录
for (int j = ; j < dataGridView.Rows.Count; j++)
{
this.dataGridView.Rows[j].Cells[cmbLinkField.Index].Value = null;
}
}
}
注:ApplicationEx.ConnectionInterface.Set/Get PropertyValue 为数据库的处理方法