大佬教程收集整理的这篇文章主要介绍了使用列过滤器时如何保留 DataGridView 颜色信息(单元格和文本)?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的项目中使用 AdvancedDataGrIDVIEw。我可以使用右键菜单更改我想要的单元格(和文本)颜色,但是当我使用列过滤器时,所有这些颜色信息都丢失了。即使我移除过滤器,这些颜色信息也不会回到原来的位置。 我设计了一个类,用于在打开/退出应用程序时保存/加载 DGV 颜色信息:
public static voID WriteDataGrIDVIEwSetTings(System.windows.Forms.DataGrIDVIEw dgv)
{
XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + @"\MyGrIDcolor.xml",null);
writer.WriteStartdocument();
writer.WriteStartElement(dgv.Name);
int LastRow = dgv.Rows.Count;
for (int i = 0; i < LastRow; i++)
{
writer.WriteStartElement("Row");
writer.WriteStartElement("Cellcolor");
writer.WriteString(dgv.Rows[i].DefaultCellStyle.BACkcolor.ToArgb().ToString());
writer.WriteEndElement();
writer.WriteStartElement("Textcolor");
writer.WriteString(dgv.Rows[i].DefaultCellStyle.Forecolor.ToArgb().ToString());
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEnddocument();
writer.Close();
}
public static voID ReadDataGrIDVIEwSetTings(System.windows.Forms.DataGrIDVIEw dgv)
{
Xmldocument xmldoc = new Xmldocument();
XmlNodeList xmlnode;
fileStream fs = new fileStream(Application.StartupPath + @"\MyGrIDcolor.xml",fileMode.open,fileAccess.Read);
xmldoc.Load(fs);
xmlnode = xmldoc.GetElementsByTagname("Row");
for (int i = 0; i <= xmlnode.Count-1; i++)
{
int cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim());
int textcolor = int.Parse(xmlnode[i].ChildNodes.Item(1).InnerText.Trim());
if (cellcolor != 0)
{
dgv.Rows[i].DefaultCellStyle.BACkcolor = color.FromArgb(Convert.ToInt32(cellcolor));
dgv.Rows[i].DefaultCellStyle.Forecolor = color.FromArgb(Convert.ToInt32(textcolor));
}
else
{
dgv.Rows[i].DefaultCellStyle.BACkcolor = color.White;
dgv.Rows[i].DefaultCellStyle.Forecolor = color.black;
}
}
fs.Close();
}
因为上面提到的问题,我在使用过滤器退出应用程序后不小心丢失了我的颜色信息。
好的,这里有一个可行的解决方案。您需要对其进行调整以满足您的确切要求,但它应该让您了解如何在重新绘制行时在 datagridview 中保留颜色。
我们的想法是我们为列表中的每一行存储一些唯一标识符,然后我们使用该标识符来决定该行是否(或什么)颜色,并将其应用于 RowPrePaint
事件。>
public partial class Form1 : Form
{
Bindingsource _bs = new Bindingsource();
//If you want to store associated colours for each row
//expand this to whatever data structure you prefer
List<int> _colourRows = new List<int>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs E)
{
//This is the normal data from,for example,a database
DataTable table = ExampleData();
//Now add an extra column and put some unique numbers in the datatable
//We do it to the underlying data
//If your data already has a unique index or sequence you don't need to do this
table.columns.Add("sequencecolumn",typeof(int));
for (int i = 0; i <= table.Rows.Count - 1; i++)
{
table.Rows[i]["sequencecolumn"] = i;
}
//Bind data to the UI
_bs.Datasource = table;
dgv.Datasource = _bs;
//Hide this column from UI
dgv.columns["sequencecolumn"].Visible = false;
dgv.SELEctionMode = DataGridViewSELEctionMode.FullRowSELEct;
}
private void setColourToolStripMenuItem_Click(object sender,EventArgs E)
{
foreach (DataGridViewRow dr in dgv.SELEctedRows)
{
//Add it to the list and also update it immediately
_colourRows.Add((int)dr.Cells["sequencecolumn"].value);
dr.DefaultCellStyle.BACkColor = Color.Red;
}
}
private void dgv_RowPrePaint(object sender,DataGridViewRowPrePaintEventArgs E)
{
//The key is that this isn't the rows position index (which could change with filtering sorTing)
//it is just a value we've added to a new column for each row
int rowColourSequence = (int)dgv.Rows[e.RowIndex].Cells["sequencecolumn"].Value;
if (_colourRows.Contains(rowColourSequencE))
{
dgv.Rows[e.RowIndex].DefaultCellStyle.BACkColor = Color.Red;
}
}
private DataTable ExampleData()
{
DataTable table = new DataTable();
table.columns.Add("Animal",typeof(String));
table.columns.Add("Count",typeof(int));
table.Rows.Add("Cat",15);
table.Rows.Add("Dog",10);
table.Rows.Add("Ferret",2);
table.Rows.Add("Mouse",0);
return table;
}
}
在这里您可以看到我是否更改了排序或过滤行颜色是否保持不变:
以上是大佬教程为你收集整理的使用列过滤器时如何保留 DataGridView 颜色信息(单元格和文本)?全部内容,希望文章能够帮你解决使用列过滤器时如何保留 DataGridView 颜色信息(单元格和文本)?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。