我的表中有一些值,我有一些问题如何改变它:
1.如果“KundeID”不是紫色的,我想改变背景,我试过--->
foreach (DataGridViewRow row1 in dataGridView2.Rows)
{
if (row1.Cells[2].Value != null)
{
row1.DefaultCellStyle.BackColor = Color.Violet;
}
}
但它会为我的所有表格着色
2.我想在对象6中有两个X,
object[] o5 = { 6, "XX", "Sprite"+"Jogurt" }; table.Rows.Add(o6);
当我有2个“Produkts”并希望在我的表格中显示它时,不知道如何
我的表:
KundeID KundeName Comment Jogurt Fanta Sprite
1 Michael "nichts" x //violet
2 Raj "Ich bin cool" //violet
Gary "yahoo" x
Miky x
5 MM x //violet
(6 XX x x) // Want to do two X's, violet
我的代码:
DataTable table = new DataTable("Kunde");
table.Columns.Add("KundeID", typeof(Int32));
table.Columns.Add("KundeName", typeof(String));
table.Columns.Add("Produkt", typeof(String));
DataTable comment = new DataTable("Comment");
comment.Columns.Add("KundeName", typeof(String));
comment.Columns.Add("Comment", typeof(String));
DataSet ds = new DataSet("DataSet");
ds.Tables.Add(table);
ds.Tables.Add(comment);
object[] o1 = { 1, "Michael", "Jogurt" };
object[] o2 = { 2, "Raj" };
object[] o3 = { null, "Gary", "Fanta" };
object[] o4 = { null, "Miky", "Sprite" };
object[] o5 = { 5, "MM", "Sprite" };
object[] c1 = { "Raj", "Ich bin cool" };
object[] c2 = { "Gary", "yahoo" };
object[] c3 = { "Michael", "nichts" };
table.Rows.Add(o1);
table.Rows.Add(o2);
table.Rows.Add(o3);
table.Rows.Add(o4);
table.Rows.Add(o5);
comment.Rows.Add(c1);
comment.Rows.Add(c2);
comment.Rows.Add(c3);
var results = from table1 in table.AsEnumerable()
join table2 in comment.AsEnumerable()
on table1.Field<string>("KundeName") equals table2.Field<string>("KundeName") into prodGroup
from table4 in prodGroup.DefaultIfEmpty()
select new
{
KundeID = table1.Field<Int32?>("KundeID"),
KundeName = table1.Field<String>("KundeName"),
Produkt = table1.Field<String>("Produkt"),
Comment = table4 != null ? table4.Field<String>("Comment") : null,
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var result = new DataTable();
result.Columns.Add("KundeID", typeof(Int32));
result.Columns.Add("KundeName", typeof(String));
result.Columns.Add("Comment", typeof(String));
result.Columns.AddRange(
(from c in
(from r in table.AsEnumerable()
where !r.IsNull("Produkt") && !string.IsNullOrEmpty(r.Field<string>("Produkt"))
select r.Field<string>("Produkt")).Distinct()
select new DataColumn(c, typeof(bool))).ToArray()
);
foreach (var r in results)
{
var productIndex = result.Columns.IndexOf(r.Produkt);
var vals = new List<object>() { r.KundeID, r.KundeName, r.Comment };
for (int i = 3; i < result.Columns.Count; i++)
{
if (i == productIndex)
{
vals.Add(true);
}
else
{
vals.Add(false);
}
}
result.LoadDataRow(vals.ToArray(), true);
}
dataGridView2.DataSource = result ;
//Here will change all background to violet, I want just some of them
//foreach (DataGridViewRow row1 in dataGridView2.Rows)
//{
// if (row1.Cells[2].Value != null)
// {
// row1.DefaultCellStyle.BackColor = Color.Violet;
// }
//}
}
答案 0 :(得分:0)
对于ASPX(网格):连接OnRowDataBound
为您的行添加颜色:
<asp:GridView ID="dataGridView2" runat="server" OnRowDataBound="RowDataBound" />
并将其放入Code-Behind文件中:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == -1)
{
return;
}
if(!string.IsNullOrEmpty(e.Row.Cells[2].Text))
{
e.Row.BackColor = Color.Violet;
}
}
对于WINFORMS:通过附加如下事件或使用设计器来连接DataBindingComplete
事件:
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
然后在处理程序中你有这样的东西:
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (e.ListChangedType != ListChangedType.ItemDeleted)
{
DataGridViewCellStyle violet = dataGridView1.DefaultCellStyle.Clone();
violet.BackColor = Color.Violet;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (r.Cells[2].Value != null)
{
r.DefaultCellStyle = violet;
}
}
}
}