Remove duplicates from listview depending on first column value - c#

What I'm trying to do is remove duplicate rows in a listView but only if the 1st column is duplicated for example:
NAME / AGE / JOB
John / 24 / Engineer
Tom / 32 / Golfer
John / 55 / Scientist
The name John is in there twice, i would just prefer to have it once, and delete all other rows, this is the basic code i have so far is:
public void RemoveDuplicates() {
for (int i = 0; i < listViewTwitter.Items.Count - 1; i++)
{
if (listViewTwitter.Items[i].Tag == listViewTwitter.Items[i + 1].Tag)
{
listViewTwitter.Items[i + 1].Remove();
}
}
}
I cannot think of the best way to do it, any help would be appreciated.

The below code is an example which I wrote for you.
To make my exmaple better, First I created a class:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Job { get; set; }
}
Then I declared a list of objects of Person class.
private void frmMain_Load(object sender, EventArgs e)
{
var list = new List<Person>()
{
new Person() { Age = 24 , Job = "Engineer", Name = "John" },
new Person() { Age = 32, Job = "Golfer", Name = "Tom " },
new Person() { Age = 55, Job = "Scientist",Name = "John" },
};
foreach (var person in list)
{
ListViewItem item = new ListViewItem(person.Name);
item.Tag = person;
listView1.Items.Add(item);
}
}
Then I remove all dupplicates by pressing a button, with two for-loop
private void btnRemoveDupplicates_Click(object sender, EventArgs e)
{
for (int i=0;i<listView1.Items.Count;i++)
{
var person = (Person)listView1.Items[i].Tag;
for (int j = 0; j < listView1.Items.Count; j++)
{
if(
((Person)listView1.Items[j].Tag).Name == person.Name &&
listView1.Items[i].Index != listView1.Items[j].Index)
{
listView1.Items[j].Remove();
continue;
}
}
}
}

Related

How to count the SubItems in a ListView?

I have diffrent entries in a ListView. I would like to count the entries (starting from the second column). The output should be under the "Total" column (see figure below, framed in red) How can this be achieved? With this code I can only access the individual rows:
for (int j = 1; j < listView1.Items[1].SubItems.Count; j++)
{
string s = listView1.Items[1].SubItems[j].Text;
}
Many thanks for your help!
Here's your solution
//iterate through all rows
for (int i = 0; i < listView1.Items.Count; i++)
{
//make sure that 5 subitems are present
int missingSubitemsCount = 5 - listView1.Items[i].SubItems.Count;
for (int j = 0; j < missingSubitemsCount; j++)
listView1.Items[i].SubItems.Add(new ListViewItem.ListViewSubItem());
int count = 0;
//test columns 1-3
for (int j = 1; j < listView1.Items[i].SubItems.Count - 1; j++)
{
//check if empty
if (String.IsNullOrEmpty(listView1.Items[i].SubItems[j].Text) == false)
count++;
}
//update last column
listView1.Items[i].SubItems[4].Text = count.ToString();
}
However, i think it would be better to use DataGridView and data binding instead. Operating directly on control can cause problems when project gets bigger. Its just better to separate data from view
Here's how i would do it with DataGridView
class MyItem
{
public string Name { get; set; }
public string Property1 { get; set; }
public string Property2 { get; set; }
public string Property3 { get; set; }
public int Total { get; set; }
}
private void button2_Click(object sender, EventArgs e)
{
var items = new List<MyItem>();
items.Add(new MyItem
{
Name = "Books",
Property1 = "A",
Property2 = "B",
Property3 = "C"
});
items.Add(new MyItem
{
Name = "Pencils",
Property1 = "A",
Property2 = "B",
});
items.Add(new MyItem
{
Name = "Tapes",
Property1 = "A",
});
//iterate through all items
foreach (var item in items)
{
//get list of MyItem properties starting with "Property"
foreach (var property in typeof(MyItem).GetProperties().Where(u => u.Name.StartsWith("Property")))
{
//test if null or empty, increment Total if not
if (property.GetValue(item) != null && String.IsNullOrEmpty(property.GetValue(item).ToString()) == false)
item.Total++;
}
//Alternative: Same calculation in one line
item.Total = typeof(MyItem).GetProperties().Count(u => u.Name.StartsWith("Property") && u.GetValue(item) != null && String.IsNullOrEmpty(u.GetValue(item).ToString()) == false);
}
//bind items to DataGridView
dataGridView1.DataSource = items;
}

C# How do I create a collection of people, then print them out sorted in a certain way?

This is my first question on this site, and I'm really stuck with a problem in C# ;
I'm trying to create a list containing x amount of people based on user input.
When the list has been generated, I need to print them out sorted based on articles sold.
Here is my problem though;
After this has been done, I also need to connect people to certain levels (Like one level is >50 articles sold, one level 51-100 articles sold etc.)
The output should look something like this:
Name District Articles sold
Rich New york 199
1 Seller has reached level 3: 100-199 articles
Mickey Alabama 202
Snoopy Texas 212
2 sellers have reached level 4: More than 199 articles
How would one go forward with this? Is the list solution in my current code a good way or not?
This is my current code:
public class newPerson
{
public string name { get; set; }
public string district { get; set; }
public string articlesSold { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("How many people do you want to add to your collection?");
int answer = Convert.ToInt32(Console.ReadLine());
List<newPerson> personList = new List<newPerson>();
for (int index = 0; index < answer; index++)
{
Console.WriteLine("Write person {0}:s name:", index + 1);
personList.Add(new newPerson()
{
name = Console.ReadLine(),
district = Console.ReadLine(),
articlesSold= Console.ReadLine(),
});
}
var sortedPersonList = personList.OrderByDescending(p => p.articlesSold);
Console.WriteLine("Name | District | Amount | ");
foreach (var p in sortedPersonList)
{
Console.WriteLine("\n");
Console.Write((p.name) + " | ");
Console.Write((p.district) + " | ");
Console.Write((p.articlesSold + " | "));
}
}
}
I was thinking about making a dictionary first instead of lists, but not sure which way to go.
If anything is unclear, I apologize. I'm very new to programming.
#Raizee, a general approach to this would be the following:
Add a Level property to each Person object which computes that person's level from how many articles they've sold.
After you've generated your List<Person> group the people together by their level.
For each level, if there's a group which exists for that level then sort that group in ascending order on the number of articles sold and output the results.
An example implementation of the above three steps would be as follows:
using System;
using System.Collections.Generic;
using System.Linq;
namespace StackOverflowSandbox.ConsoleApp
{
public class Person
{
public string Name { get; set; }
public string District { get; set; }
public int ArticlesSold { get; set; }
public int Level
{
get
{
if (ArticlesSold > 199) // 200 and above
return 4;
if (ArticlesSold > 99) // [100,199]
return 3;
if (ArticlesSold > 49) // [50,99]
return 2;
return 1; // 49 and below
}
}
}
class Program
{
static void Main(string[] args)
{
var people = new List<Person>()
{
new Person() { Name = "Bob", District = "London", ArticlesSold = 500 },
new Person() { Name = "Alice", District = "Detroit", ArticlesSold = 125 },
new Person() { Name = "Jane", District = "New York", ArticlesSold = 150 },
new Person() { Name = "Alex", District = "5", ArticlesSold = 500 }
};
var peopleGroupedByLevel = people
.GroupBy(p => p.Level)
.ToDictionary(g => g.Key, g => g.ToList());
Console.WriteLine("Name | District | Amount");
if (peopleGroupedByLevel.ContainsKey(1))
{
var levelOnePeople = peopleGroupedByLevel[1];
Console.WriteLine($"{levelOnePeople.Count} Seller has reached level 1: less than 50 articles");
var levelOnePeopleSortedByArticlesSold = levelOnePeople.OrderBy(p => p.ArticlesSold);
PrintPeopleToConsole(levelOnePeopleSortedByArticlesSold);
}
if (peopleGroupedByLevel.ContainsKey(2))
{
var levelTwoPeople = peopleGroupedByLevel[2];
Console.WriteLine($"{levelTwoPeople.Count} Seller has reached level 2: 50-99 articles");
var levelTwoPeopleSortedByArticlesSold = levelTwoPeople.OrderBy(p => p.ArticlesSold);
PrintPeopleToConsole(levelTwoPeopleSortedByArticlesSold);
}
if (peopleGroupedByLevel.ContainsKey(3))
{
var levelThreePeople = peopleGroupedByLevel[3];
Console.WriteLine($"{levelThreePeople.Count} Seller has reached level 3: 100-199 articles");
var levelTwoPeopleSortedByArticlesSold = levelThreePeople.OrderBy(p => p.ArticlesSold);
PrintPeopleToConsole(levelTwoPeopleSortedByArticlesSold);
}
if (peopleGroupedByLevel.ContainsKey(4))
{
var levelFourPeople = peopleGroupedByLevel[4];
Console.WriteLine($"{levelFourPeople.Count} Seller has reached level 4: More than 199 articles");
var levelFourPeopleSortedByArticlesSold = levelFourPeople.OrderBy(p => p.ArticlesSold);
PrintPeopleToConsole(levelFourPeopleSortedByArticlesSold);
}
}
private static void PrintPeopleToConsole(IEnumerable<Person> people)
{
foreach (var person in people)
{
Console.WriteLine($"{person.Name} | {person.District} | {person.ArticlesSold}");
}
}
}
}
Note that in this example I hard-coded the input List<Person> because I didn't want to have to mess around repeatedly inputting the people.
I have rewritten a little bit:
public class Person
{
public string Name { get; set; }
public string District { get; set; }
public int ArticlesSold { get; set; }
public int Level
{
get
{
if (ArticlesSold <= 50) return 1;
if (ArticlesSold > 50 && ArticlesSold <= 100) return 2;
return 0;
}
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("How many people do you want to add to your collection?");
var count = Convert.ToInt32(Console.ReadLine());
var personList = new List<Person>();
for (var index = 1; index <= count; index++)
{
Console.WriteLine("Write person {0}:s name:", index + 1);
personList.Add(new Person()
{
Name = Console.ReadLine(),
District = Console.ReadLine(),
ArticlesSold = Convert.ToInt32(Console.ReadLine()),
});
}
var sortedPersonList = personList.OrderByDescending(p => p.ArticlesSold);
Console.WriteLine("Name | District | Amount | Level | ");
foreach (var p in sortedPersonList)
{
Console.WriteLine("\n");
Console.Write((p.Name) + " | ");
Console.Write((p.District) + " | ");
Console.Write((p.ArticlesSold + " | "));
Console.Write((p.Level + " | "));
}
}
}
You could have a method in your newPerson class called CalculateLevel. This returns the level after you call it in your Console.Write. For example:
public int CalculateLevel()
{
if (articlesSold <= 50) return 1;
}
Then in your print method you can call it like this:
Console.WriteLine($"Level: {p.CalculateLevel()}")

Error Cannot implicitly convert type 'string' to 'int' - for the iterator in a ForEach loop

My code posted below, does not run, due to an
Error - Cannot implicitly convert type 'string' to 'int'
This error occurs to the iterator i within both my if conditions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaleOrFemale
{
class Program
{
static void Main(string[] args)
{
string[,] names = new string [7,2] {{"Cheryl Cole","F"},{"Hilary Clinton","F"},{"Noel Gallagher","M"},{"David Cameron","M"},{"Madonna","F"},{"Sergio Aguero","M"},{"Sheik Mansour","M"}};
int mCount = 0;
int fCount = 0;
foreach ( var i in names)
{
if (names[i,1]== "M")
{
mCount += 1;
}
else if (names[i,1]=="F")
{
fCount += 1;
}
}
Console.WriteLine("mCount = {0}", mCount);
Console.WriteLine("fCount = {0}", fCount);
Console.Read();
}
}
}
Why can my array index not use the foreach Iterator in this case?
For your particular case, there is no need for foreach loop, foreach will iterate each element in your multi-dimensional array and return each element one by one. That is why you are getting the error. Since i will be string value on each iteration and you are trying to use it in array index, which can only accept an integer value.
You can fix this by using a simple for loop like:
for (var i = 0; i < names.GetLength(0); i++)
{
if (names[i, 1] == "M")
{
mCount += 1;
}
else if (names[i, 1] == "F")
{
fCount += 1;
}
}
Notice the GetLength method, it will return the length of specified dimension.
But a better option would be to use a class for your data, instead of multi-dimensional array, and even an enum for gender.
public class MyDataClass
{
public string Name { get; set; }
public Gender Gender { get; set; }
}
public enum Gender
{
Male,
Female,
Other,
}
And then you can do:
List<MyDataClass> list = new List<MyDataClass>
{
new MyDataClass {Name = "Cheryl Cole", Gender = Gender.Female},
new MyDataClass {Name = "Hilary Clinton", Gender = Gender.Female},
new MyDataClass {Name = "Noel Gallagher", Gender = Gender.Female},
new MyDataClass {Name = "David Cameron", Gender = Gender.Male},
new MyDataClass {Name = "Madonna", Gender = Gender.Female},
new MyDataClass {Name = "Sergio Aguero", Gender = Gender.Male},
new MyDataClass {Name = "Sheik Mansour", Gender = Gender.Male},
};
foreach (var item in list)
{
if (item.Gender == Gender.Male)
mCount += 1;
if (item.Gender == Gender.Female)
fCount += 1;
}
You are trying to access the array via indexes, indexes are meant to be integers.
Here when you call names[i,1], then i should be integer not string. When you have foreach ( var i in names) then i is inferred to be string because name is array of type string[,].
If you want to access each element via index try using a for loop instead
for(var i = 0; i < names.GetLength(0); i++)
{
if (names[i,1]== "M")
{
mCount += 1;
}
else if (names[i,1]=="F")
{
fCount += 1;
}
}
Keep in mind that when you use foreach you are accessing elements not the indexes.
But best way here is to define a class instead of holding data into multi-dimensional arrays.
public class Person
{
public string Name { get; set; }
public Gender Gender { get; set; }
}
public enum Gender
{
Male,
Female
}
and the program will looks like this
var people = new List<Person>();
people.Add(new Person(){ Name = "Cheryl Cole", Gender = Gender.Female });
people.Add(new Person(){ Name = "Hilary Clinton", Gender = Gender.Female });
people.Add(new Person(){ Name = "Noel Gallagher", Gender = Gender.Male });
int mCount = 0;
int fCount = 0;
foreach (var person in people)
{
if (person.Gender == Gender.Male)
mCount += 1;
else if (person.Gender == Gender.Female)
fCount += 1;
}
Console.WriteLine("mCount = {0}", mCount);
Console.WriteLine("fCount = {0}", fCount);
Console.Read();
or you can use linq for the whole concept of counting items with specific conditions
Console.WriteLine("#Male = {0}", people.Count(x=>x.Gender == Gender.Male));
Console.WriteLine("#Female = {0}", people.Count(x=>x.Gender==Gender.Female));
Your iterator will return an array, not an int.
Try something like this:
foreach ( var i in names)
{
if (i[1]== "M")
{
mCount += 1;
}
else if (i[1]=="F")
{
fCount += 1;
}
}
Although as others mentioned, this is a sloppy way of doing it. Better to use structs and/or custom classes and/or enums.
var i dose not contain int value. So cannot access like this names[i,1].So your implementation should be changed like as follows.
foreach ( var i in names){
if (i[1] == "M"){
mCount++;
}
else if (i[1] == "F"){
fCount++;
}
}
At compile-time, the variable i is determined as a String. So using the foreach iterator isn't going to work with your current implementation.
Alternatively, you can use the for iterator with the following code:
string[,] names = new string[7, 2] { { "Cheryl Cole", "F" }, { "Hilary Clinton", "F" }, { "Noel Gallagher", "M" }, { "David Cameron", "M" }, { "Madonna", "F" }, { "Sergio Aguero", "M" }, { "Sheik Mansour", "M" } };
int mCount = 0;
int fCount = 0;
int length = names.GetLength(0);
for (int i = 0; i < length; i++)
{
if (names[i, 1] == "M")
{
mCount += 1;
}
else if (names[i, 1] == "F")
{
fCount += 1;
}
}
Console.WriteLine("mCount = {0}", mCount);
Console.WriteLine("fCount = {0}", fCount);
Console.Read();
which outputs:
mCount = 4
fCount = 3
The above code uses names.GetLength(0); in order to get the length of the array at dimension 0. In our case, this outputs 7.
Since you are using foreach to loop, your counter variable (i) is being set to a string (value) from the array...
Use a for loop instead...
for(int i=0; i< names.GetLength(0);i++)
You should explore the option of using classes and LINQ to make the code cleaner and maintainable.
using System;
using System.Collections.Generic;
using System.Linq;
public enum Gender
{
Female, Male
}
public class Person
{
public string Name { get; private set; }
public Gender Gender { get; private set; }
public Person(string name, Gender gender)
{
Name = name;
Gender = gender;
}
}
public class Program
{
public static void Main()
{
var persons = new[] { new Person("Cheryl Cole", Gender.Female), new Person("David Cameron", Gender.Male) };
var mCount = persons.Count(p => p.Gender == Gender.Male);
var fCount = persons.Count(p => p.Gender == Gender.Female);
Console.WriteLine("mCount = {0}", mCount);
Console.WriteLine("fCount = {0}", fCount);
}
}

How to allow user to change list box order

I am looking for help, I have two lists that both add data to the same list box and it displays them as a summary, I would like to know how to let the user move an index up or down in the list box.
Items are added here
private void BtnAddpickup_Click(object sender, EventArgs e)
{
/*
* This method creates a new pickup object, allows the user to
* enter details and adds it to the List
*
*/
Pickupform.pickup = new Pickups();
//New Visit- note added to the pickupform object
Pickupform.ShowDialog();
//Show the pickupForm. ShowDialog ensures that the form has the exclusive focus until it is closed.
if (Pickupform.pickup != null)
//if null then the "cancel" button was pressed
{
Pickups newpic = Pickupform.pickup;
//Get the Pickup object from the form
thePickup.addPickups(newpic);
//Add the visit to the list
}
updateList();
//Update the list object to reflect the Pickups in the list
}
and this
public Pickups getPickups(int index)
{
//Return the pickup object at the <index> place in the list
int count = 0;
foreach (Pickups pic in pickups)
{
//Go through all the pickup objects
if (index == count)
//If we're at the correct point in the list...
return pic;
//exit this method and return the current visit
count++;
//Keep counting
}
return null;
//Return null if an index was entered that could not be found
}
This is the same for my other class, So any help would be appreciated
You can try something like this. The following code assumes a Windows form containing a ListBox named mainListBox, a button named upButton, and a button named downButton.
public partial class Form1 : Form
{
private class Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
public override string ToString()
{
return string.Format("{0}, {1}", LastName, FirstName);
}
}
public Form1()
{
this.InitializeComponent();
this.mainListBox.SelectionMode = SelectionMode.One;
this.PopulateListBox();
}
private void PopulateListBox()
{
this.mainListBox.Items.Add(new Person() { FirstName = "Joe", LastName = "Smith" });
this.mainListBox.Items.Add(new Person() { FirstName = "Sally", LastName = "Jones" });
this.mainListBox.Items.Add(new Person() { FirstName = "Billy", LastName = "Anderson" });
}
private void upButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > 0)
{
int selectedIndex = this.mainListBox.SelectedIndex;
object selectedItem = this.mainListBox.SelectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex - 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex - 1;
}
}
private void downButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > -1 &&
this.mainListBox.SelectedIndex < this.mainListBox.Items.Count - 1)
{
int selectedIndex = this.mainListBox.SelectedIndex;
object selectedItem = this.mainListBox.SelectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex + 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex + 1;
}
}
}
This will only work if you are adding items to the ListBox using the ObjectCollection.Add method. If you are data binding, you can update the actual data source and use the ListBox's BindingContext to refresh.
private List<Person> people = new List<Person>();
private void PopulateListBox()
{
this.people.Add(new Person() { FirstName = "Joe", LastName = "Smith" });
this.people.Add(new Person() { FirstName = "Sally", LastName = "Jones" });
this.people.Add(new Person() { FirstName = "Billy", LastName = "Anderson" });
this.mainListBox.DataSource = people;
}
private void upButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > 0)
{
int selectedIndex = this.mainListBox.SelectedIndex;
Person selectedItem = this.mainListBox.SelectedItem as Person;
this.people.RemoveAt(selectedIndex);
this.people.Insert(selectedIndex - 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex - 1;
this.RefreshListSource();
}
}
private void RefreshListSource()
{
CurrencyManager boundList = this.mainListBox.BindingContext[this.people] as CurrencyManager;
boundList.Refresh();
}

Devexpress GridView conditional cell formatting

There is a gridView with Items to buy.
Group
- Checkbox|Item Description
And there is a maximum of items that can be bought on each group.
I want to change the appearance of all not selected rows, when the max is reached (per group).
example:
Select 1 item from each group
Group 1
[ ] Item 1
[ ] Item 2
Group 2
[ ] Item 3
[ ] Item 4
[ ] Item 5
After selection
Group 1
[x] Item 1
[ ] Item 2
Group 2
[ ] Item 3
[x] Item 4
[ ] Item 5
After the max amount of items on each group is checked, I want to alter the appearance of the rest of the items.
I have a group summary for the first column. My problem is that I don't know how to trigger the appearance change of all cells. Is it correct to count selected items on each cell-leave event or is there a better way to accomplish this?
I created Devexpress template with GridControl.
Person class was created for me.
I changed it a little for this example.
public class Person {
public Person(string firstName, string secondName) {
this.FirstName = firstName;
this.SecondName = secondName;
this.Comments = String.Empty;
}
public Person(string firstName, string secondName, string comments)
: this(firstName, secondName) {
this.Comments = comments;
}
public bool Selected
{
get;
set;
}
public bool Blocked
{
get;
set;
}
public string FirstName
{
get;
set;
}
public string SecondName
{
get;
set;
}
public string Comments
{
get;
set;
}
}
My grid looks like this:
And I achived Your functionality with code:
public partial class Form1 : XtraForm
{
int max = 2;
public Form1()
{
InitializeComponent();
InitGrid();
}
List<Person> gridDataList = new List<Person>();
void InitGrid()
{
gridDataList.Add(new Person("John", "Smith"));
gridDataList.Add(new Person("Gabriel", "Smith"));
gridDataList.Add(new Person("Ashley", "Smith", "some comment"));
gridDataList.Add(new Person("Adrian", "Smith", "some comment"));
gridDataList.Add(new Person("Gabriella", "Smith", "some comment"));
gridDataList.Add(new Person("John", "Forester"));
gridDataList.Add(new Person("Gabriel", "Forester"));
gridDataList.Add(new Person("Ashley", "Forester", "some comment"));
gridDataList.Add(new Person("Adrian", "Forester", "some comment"));
gridDataList.Add(new Person("Gabriella", "Forester", "some comment"));
bindingSource1.DataSource = gridDataList;
}
private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
int parentHandle = gridView1.GetParentRowHandle(e.RowHandle);
int count = gridView1.GetChildRowCount(parentHandle);
int childHandle = -1;
int nCount = 0;
for (int i = 0; i < count; i++)
{
childHandle = gridView1.GetChildRowHandle(parentHandle, i);
Person p = gridView1.GetRow(childHandle) as Person;
if (p != null)
{
p.Blocked = false;
if (p.Selected)
{
nCount++;
}
}
}
if (nCount == max)
{
for (int i = 0; i < count; i++)
{
childHandle = gridView1.GetChildRowHandle(parentHandle, i);
Person p = gridView1.GetRow(childHandle) as Person;
if (p != null && !p.Selected)
{
p.Blocked = true;
}
}
}
// to redraw grid
gridView1.RefreshData();
}
private void richedSelected_EditValueChanged(object sender, EventArgs e)
{
gridView1.PostEditor();
}
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
Person p = gridView1.GetRow(e.RowHandle) as Person;
if (p != null && p.Blocked)
{
e.Appearance.ForeColor = Color.White;
}
}
private void richedSelected_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
Person p = gridView1.GetRow(gridView1.FocusedRowHandle) as Person;
if (p != null && p.Blocked)
{
e.Cancel = true;
}
}
}
This is of course simplified implementation. Just to get You on the right track.
Elements from designer:
private DevExpress.XtraGrid.GridControl gridControl;
private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
private System.Windows.Forms.BindingSource bindingSource1;
private DevExpress.XtraGrid.Columns.GridColumn colFirstName;
private DevExpress.XtraGrid.Columns.GridColumn colSecondName;
private DevExpress.XtraGrid.Columns.GridColumn colComments;
private DevExpress.XtraGrid.Columns.GridColumn colSelected;
private DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit richedSelected;
If You find any better solution please let me know.
Here is an example for what I needed link

Categories

Resources