[英]changing color of particular row in datagrid C# windows ce compact framework
I am developing ac# application on my windows ce handheld with compact framework 2.0 . 我正在使用紧凑型框架2.0在Windows CE手持设备上开发ac#应用程序。 I have an grid like one below
我下面有一个网格
name location
----------------------
John US
Flex UK
I have used datagrid for the grid formation . 我已经使用datagrid进行网格形成。 Note : DataGrid(new System.Windows.Forms.DataGrid()) not DataGridView .
注意:DataGrid(new System.Windows.Forms.DataGrid())不是DataGridView。 There is an text box above the grid.
网格上方有一个文本框。 If the user keys in uk in the text box i have to change the color of the second row in the datagrid.
如果用户在文本框中键入uk,则必须更改数据网格中第二行的颜色。 How do i acheive it ??
我如何实现?
private System.Windows.Forms.DataGrid scnDataGrd = new System.Windows.Forms.DataGrid();
I have addded an coloumn named status . 我添加了一个名为status的栏目。 And i want to display an image over there.If the text from the textbox matches.
我想在那边显示图像。如果文本框中的文本匹配。
Adding coloumns to tables and filling values based on csv : 在表中添加列并基于csv填充值:
if (data.StartsWith("Coloumns"))
data = data.Substring(index + 1, data.Length - (index + 1));
data = data.Trim();
string[] values = data.Split(',');
// Add the index on the column on which data is stored in csv
List<int> csvIndex = new List<int>();
for (int i = 0; i < values.Length; i++)
{
values[i] = values[i].Trim();
if (values[i] != "Status")
csvIndex.Add(csvHeaders.IndexOf(values[i]));
}
rfidindex = (csvHeaders.IndexOf("RFID") == -1) ? csvHeaders.IndexOf("rfid") : csvHeaders.IndexOf("RFID");
//Image myImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("search.png"));
//ImageConverter imageConverter = new ImageConverter();
Image img = AssetGatherMobileQuickTraQ.Properties.Resources.search;
//Properties.Resources.my_image;
string csvdata = strRdr.ReadToEnd();
csvdata = csvdata.Replace('\r', ' ');
string[] tagDets = csvdata.Split(new char[] { '\n' });
csvItems = new Hashtable();
DataTable dataTable1 = (DataTable)scnDataGrd.DataSource;
for (int i = 0; i < tagDets.Length; i++)
{
string[] individTagDet = tagDets[i].Split(new char[] { ',' });
if (individTagDet.Length <= 1)
break;
csvItems.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim(), individTagDet);
DataRow row = dataTable1.NewRow();
row.BeginEdit();
//System.Drawing.Image imgTest = System.Drawing.Image. FromFile("C:\\Test.jpg");
//System.Drawing.Image img = System.Drawing.Image.ReferenceEquals
//dataTable1.Columns
Bitmap image1 = new System.Drawing.Bitmap(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\search.png");
//scnDataGrd.Dara
row[0] = image1;
//DataGrid
for (int j = 0; j < 3; j++)
{
row[j+1] = individTagDet[csvIndex[j]].Replace("\"",string.Empty).Trim();
}
//scnDataGrd.TableStyles
csvRfidList.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim());
missingTagList.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim());
row.EndEdit();
dataTable1.Rows.Add(row);
scnDataGrd.DataSource = dataTable1;
}
If the text maches it arrives here : 如果文本匹配,它将到达此处:
DataTable dataTable = (DataTable)scnDataGrd.DataSource;
int index1 = -1;
bool found = false;
DataGridTableStyle ts = new DataGridTableStyle();
foreach (DataRow dr in dataTable.Rows)
{
index1++;
if (dr[tableColIndex].ToString() == textValue)
{
scnDataGrd.Select(index1);
scnDataGrd.SelectionForeColor = Color.Red;
break;
}
else {
dr[0] = "Not Found";
}
}
In Status Coloumn, Instead of image it displays as System.Data.Bitmap 在状态列中,而不是图像,它显示为System.Data.Bitmap
try this for change backColor 试试这个改变backColor
int index = -1;
bool found = false;
foreach (DataRow dr in myDataSet.Tables[0].Rows)
{
index++;
string d = dr["location"].ToString();
if (dr[0].ToString() == txtbox1,Text)
{
found=true;
break;
}
}
if(found)
{
scnDataGrd.Select(index);
scnDataGrd.SelectionBackColor = Color.Blue;
}
if you want to change fore color replace SelectionBackColor to SelectionForeColor . 如果要更改前景色,请将SelectionBackColor替换为SelectionForeColor 。 Here myDataSet is a binding element of DataGrid from this first we find location of Data, after that select that row from
这里myDataSet是DataGrid的绑定元素,首先我们找到Data的位置,然后从中选择该行
scnDataGrd.Select(index);
and change colour of selecting row from 并更改从中选择行的颜色
scnDataGrd.SelectionForeColor = Color.Blue;
hope it helps. 希望能帮助到你。
EDIT 编辑
you can update font color by replacing 您可以通过替换来更新字体颜色
scnDataGrd.SelectionBackColor = Color.Blue;
to 至
scnDataGrd.SelectionForeColor = Color.Blue;
you can't give image like that, you have to make custom image view for that, but if you want thisfrom check box, you can implement like that : 您不能提供这样的图像,您必须为此创建自定义图像视图,但是如果您希望fromfrom复选框,则可以这样实现:
if (data.StartsWith("Coloumns"))
data = data.Substring(index + 1, data.Length - (index + 1));
data = data.Trim();
string[] values = data.Split(',');
// Add the index on the column on which data is stored in csv
List<int> csvIndex = new List<int>();
for (int i = 0; i < values.Length; i++)
{
values[i] = values[i].Trim();
if (values[i] != "Status")
csvIndex.Add(csvHeaders.IndexOf(values[i]));
}
rfidindex = (csvHeaders.IndexOf("RFID") == -1) ? csvHeaders.IndexOf("rfid") : csvHeaders.IndexOf("RFID");
//Image myImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("search.png"));
//ImageConverter imageConverter = new ImageConverter();
Image img = AssetGatherMobileQuickTraQ.Properties.Resources.search;
//Properties.Resources.my_image;
string csvdata = strRdr.ReadToEnd();
csvdata = csvdata.Replace('\r', ' ');
string[] tagDets = csvdata.Split(new char[] { '\n' });
csvItems = new Hashtable();
DataTable dataTable1 = (DataTable)scnDataGrd.DataSource;
for (int i = 0; i < tagDets.Length; i++)
{
string[] individTagDet = tagDets[i].Split(new char[] { ',' });
if (individTagDet.Length <= 1)
break;
csvItems.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim(), individTagDet);
DataRow row = dataTable1.NewRow();
row.BeginEdit();
//System.Drawing.Image imgTest = System.Drawing.Image. FromFile("C:\\Test.jpg");
//System.Drawing.Image img = System.Drawing.Image.ReferenceEquals
//dataTable1.Columns
Bitmap image1 = new System.Drawing.Bitmap(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\search.png");
//scnDataGrd.Dara
row[0] = image1;
//DataGrid
for (int j = 0; j < 3; j++)
{
row[j+1] = individTagDet[csvIndex[j]].Replace("\"",string.Empty).Trim();
}
//scnDataGrd.TableStyles
csvRfidList.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim());
missingTagList.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim());
row.EndEdit();
dataTable1.Rows.Add(row);
DataColumn cCurrent = new DataColumn("Current", typeof(bool));
dataTable1.Rows.Add(cCurrent);
scnDataGrd.DataSource = dataTable1;
}
and in your criteria, where you make condition implement like this.. 并在您的条件中,使条件实现像这样。
DataTable dataTable = (DataTable)scnDataGrd.DataSource;
int index1 = -1;
bool found = false;
DataGridTableStyle ts = new DataGridTableStyle();
foreach (DataRow dr in dataTable.Rows)
{
index1++;
if (dr[tableColIndex].ToString() == textValue)
{
scnDataGrd.Select(index1);
scnDataGrd.SelectionForeColor = Color.Red;
dr["Current"] = true;
break;
}
else
{
dr[0] = "Not Found";
}
}
where current in 当前在哪里
dr["Current"] = true;
dr [“ Current”] = true;
is DataColumn which we implemented before in third last line. 是我们在最后第三行中实现的DataColumn。 on previous code.
在之前的代码上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.