Datagrid column width and row color.

VS 2003 VB .NET

I am currently trying to accomplish two things.
1. In Code, trying to set the column width
2. Trying to change the color of the row based on the value of one cell.

For the width, I thought I could do it like this:

Dim ts As DataGridTableStyle = New DataGridTableStyle
Dim daGrid1 As New SqlDataAdapter(Me.SqlCommand1)
dsGrid1 = New DataSet
dsGrid1.Clear()
daGrid1.Fill(dsGrid1, "top")
DataGrid1.DataSource = dsGrid1.Tables(0).DefaultView
DataGrid1.TableStyles.Add(ts)
DataGrid1.TableStyles(0).GridColumnStyles(1).Width = 5

This does not effect the grid at all.

2. I don't know where to start, can't find a walkthrough or article on just this.

I am needing to do both of these at run time, not design time.

Any thoughts
Thanks



Answer this question

Datagrid column width and row color.

  • BobConsultant

    Sam,
    I have used the method you are referring to above, and successfully color one cell. I just can't get it to do the whole row from that paint method.


  • Umachandar Jayachandran - MS

    Try datagrid1.tablestyles.clear before you add dts


  • KHadden

    I'm also looking for a way to color the entire row based on the contents of a cell. If you find out, please let me know!


  • regthesk8r

    Here are a few C# snippets. During initialization, I make a Style with the colored background:

    dataGridViewCellStyle = mainDataGridView.DefaultCellStyle.Clone();

    Color color = Color.FromName(theColorNode.Value);

    dataGridViewCellStyle.BackColor = color;

    styles[theValue] = dataGridViewCellStyle;

    When the row scrolls into view, I set the background color:

    mainDataGridView.Rows[rowNumber].DefaultCellStyle = ((DataGridViewCellStyle)(columnsIdea as Column).styles[str]);

    Hope this helps

    Mike


  • jag108

    I am trying from the examples, and so far have this;

    Dim dts As DataGridTableStyle = New DataGridTableStyle
    Dim daGrid1 As New SqlDataAdapter(Me.SqlCommand1)
    dsGrid1 = New DataSet

    dsGrid1.Clear()
    daGrid1.Fill(dsGrid1, "top")
    dts.MappingName = dsGrid1.Tables("top").TableName
    Dim colTask As New DataGridTextBoxColumn
    With colTask
    .MappingName = "Task"
    .HeaderText = "Task"
    .Width = 10
    End With
    dts.GridColumnStyles.Add(colTask)
    DataGrid1.DataSource = dsGrid1.Tables(0).DefaultView
    DataGrid1.TableStyles.Add(dts)

    but this is the error I am getting:

    "The data grid table styles collection already contains a table style with the same mapping name."

  • CHEN YU-TIEN

    here is what you have to do:

    1 All of your column must be an instance of the custom column style class

    2. You can check the value of another column in the Pain method. Thus, based on another column value, you can paint the current column with different color.

    protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight)

    {

    //Add this section, in this example if the value of the cell at column 1 is "TRUE", then the entire row will be red

    string isvip=this.DataGridTableStyle.DataGrid[RowNum,1].ToString().ToUpper();

    if(isvip=="TRUE")

    BackBrush = Brushes.Red;

    else

    BackBrush = Brushes.White;

    //

    }

    hope this helps

    ~Sam



  • cisco0407

    check this out:

    http://www.windowsforms.net/FAQs/default.aspx PageID=2&ItemID=412&CategoryID=3&tabindex=3

    but, this shows only how to color a particular cell based on its value.  If you modify the Paint  method in your custom class, you can color the entire row based on the value of one cell.

     

     

    hope this helps

     

    ~Sam



  • Romantic_touch

    Yes! Thank you.
    All the examples I had from doing searches must not have had this issue. They showed me the proper syntax, just not this.

    (Any idea on changing color for each row based on a value of one column in that row )

    Thanks,
    Steve

  • CharlieRussell

    You need to set the datagrid table styles mapping name for the table style to work. You will find some datagrid examples on the vb-tips website.


  • ScottPGlover

    I found out how to color one cell based on the value:

    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q1075q
    (5.14)

    Can't get it to color the whole row, but this should work for now. If anyone has go it to color the entire row, pls reply.


  • Datagrid column width and row color.