You must Sign In to post a response.
  • Category: .NET

    How to Set Coloring and Font Style and Alignment etc

    Hi

    I have Design ListView in C# windows Application.
    How to Set Coloring and Font Style and Alignment etc in particular cell

    My Data this

    Name Empno Status
    aa 100 OK
    bb 200 NOTOK

    1.here status is which is ok that cell value font color with bold need . How will do this
    2.when i selected row i need coloring the selected row in listview.
  • #767641
    you can set different font size and style to each listview items, see below snippet

    private void InitializeListView()
    {

    // Set the Location, View and Width properties for the
    // ListView object.
    myListView = new ListView();
    myListView.Location = new System.Drawing.Point(20, 20);
    myListView.Width = 250;

    // The View property must be set to Details for the
    // subitems to be visible.
    myListView.View = View.Details;

    // Each SubItem object requires a column, so add three columns.
    this.myListView.Columns.Add("Key", 50, HorizontalAlignment.Left);
    this.myListView.Columns.Add("A", 100, HorizontalAlignment.Left);
    this.myListView.Columns.Add("B", 100, HorizontalAlignment.Left);

    // Add a ListItem object to the ListView.
    ListViewItem entryListItem = myListView.Items.Add("Items");

    // Set UseItemStyleForSubItems property to false to change
    // look of subitems.
    entryListItem.UseItemStyleForSubItems = false;

    // Add the expense subitem.
    ListViewItem.ListViewSubItem expenseItem =
    entryListItem.SubItems.Add("Expense");

    // Change the expenseItem object's color and font.
    expenseItem.ForeColor = System.Drawing.Color.Red;
    expenseItem.Font = new System.Drawing.Font(
    "Arial", 10, System.Drawing.FontStyle.Italic);

    // Add a subitem called revenueItem
    ListViewItem.ListViewSubItem revenueItem =
    entryListItem.SubItems.Add("Revenue");

    // Change the revenueItem object's color and font.
    revenueItem.ForeColor = System.Drawing.Color.Blue;
    revenueItem.Font = new System.Drawing.Font(
    "Times New Roman", 10, System.Drawing.FontStyle.Bold);

    // Add the ListView to the form.
    this.Controls.Add(this.myListView);
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767642
    Hi

    your code not working. I need windows application C# particular cell text fore color changed in runtime.
    which is status=OK

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #767655
    Hi,

    Try below sample


    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
    var dataItem = e.Item as ListViewDataItem;
    if (dataItem != null)
    {
    string Status = (string)ListView1.DataKeys[dataItem.DisplayIndex]["Status"];
    if (Status == "OK")
    e.item.attributes.add("style", "background-color: red");
    }
    }


    Hope this helps you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767658
    Hi
    Naveen

    check My question I mention like this I have Design ListView in C# windows Application.

    I need only Windows application. ItemDataBound Event only Web . can you send corresponding
    windows application.

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #767669
    Hai Prasad,
    You need to first need to make the default style for the subitem to be false and then you can apply the other styles like:

    ListView1.Items[0].UseItemStyleForSubItems = false;
    ListView1.Items[0].SubItems[1].ForeColor = Color.Blue;

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments