我有一个列表视图,该视图从数据表中提取数据行。通常,第一列“ RuleActive”将使用文本值“ True”或“ False”填充列单元格
我已将“ True”或“ False”值替换为绿色或红色圆圈,作为每次我重新加载列表视图时都会动态更新的图标。这部分工作正常。我现在想做的是将图像水平放置在像元的中心,当前它放置在Far LHS上。
我读过的所有文章都没有给我解决方案。我已经注释掉配置图像的代码行(请参阅“帮助”,“帮助”!)我猜我需要在本节中添加一些其他代码,以将图像推到单元格的中心位置。
谢谢!
public void Update_RulesListView()
{
try
{
// Momentarialy clear the List View so it can be re-populated.
// THIS IS A CRITICAL ONE FOR CORRECT OPERATION - DO NOT REMOVE!
RulesListView.Clear();
RulesListView.Columns.Add("Rule Active:", 100, HorizontalAlignment.Left);
RulesListView.Columns.Add("Rule Name:", 200, HorizontalAlignment.Left);
RulesListView.Columns.Add("System Mapping:", 200, HorizontalAlignment.Left);
//RulesListView.Columns.Add("INBOUND Module:", 140, HorizontalAlignment.Left);
//RulesListView.Columns.Add("Type:", 110, HorizontalAlignment.Left);
//RulesListView.Columns.Add("OUTBOUND Module:", 150, HorizontalAlignment.Left);
//RulesListView.Columns.Add("Type:", 110, HorizontalAlignment.Left);
RulesListView.Columns.Add("CreateDate:", 180, HorizontalAlignment.Left);
RulesListView.Columns.Add("LastUpdated:", 180, HorizontalAlignment.Left);
// Create the referece to the images from propeties resources.
Image greenCircle = new Bitmap(Properties.Resources.GreenCircle);
Image redCircle = new Bitmap(Properties.Resources.RedCircle);
// Create an ImageList object.
ImageList imageListSmall = new ImageList { ImageSize = new Size(15, 15) };
// Add the images to the image list.
imageListSmall.Images.Add(greenCircle);
imageListSmall.Images.Add(redCircle);
// Define the image list used by the list view.
RulesListView.SmallImageList = imageListSmall;
for (int i = 0; i < SystemMapping.SystemMappingDataTable.Rows.Count; i++)
{
DataRow dataRow = DB_Rules.RulesDataTable.Rows[i];
ListViewItem listitem = new ListViewItem(dataRow["RuleActive"].ToString());
string ruleActive = (dataRow["RuleActive"].ToString());
if (ruleActive == "True")
{
listitem.Text = "";
listitem.ImageIndex = 0;
/// HELP HELP HELP! I need this image to be positioned in the center of the cell...
}
if (ruleActive == "False")
{
listitem.Text = "";
listitem.ImageIndex = 1;
}
listitem.SubItems.Add(dataRow["RuleName"].ToString());
listitem.SubItems.Add(dataRow["SystemMapping"].ToString());
listitem.SubItems.Add(dataRow["CreateDate"].ToString());
listitem.SubItems.Add(dataRow["LastUpdated"].ToString());
RulesListView.Items.Add(listitem);
}
}
catch (Exception ex)
{
log.Error(ex);
}
}