Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > DataGrid row selection help !!!!
 

DataGrid row selection help !!!!

Hi All, 
I might have a stupid question but I really need to know how can I make a full row selection of the datagrid like TaskVision?????
MigrationUser 1  Tuesday, May 13, 2003 2:05 PM
Well, I don't know if this will help but you just use the MouseUp (or other related) event... as I've done below.  If this is not what you're after perhaps you need to add some detail on what you're trying to achieve.

HTH...

Mike.

<Code>
  Private Sub dgrdOrders_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles dgrdOrders.MouseUp

    'Create a HitTestInfo object using the HitTest method.
    'Get the DataGrid by casting sender.
    Dim myGrid As DataGrid = CType(sender, DataGrid)
    Dim htiUsers As DataGrid.HitTestInfo = myGrid.HitTest(e.X, e.Y)
    Dim strItem As String
    Dim intQuantity As Integer

    Select Case htiUsers.Type

      'Case System.Windows.Forms.DataGrid.HitTestType.Cell

    Case System.Windows.Forms.DataGrid.HitTestType.RowHeader

        intSelectedRow = htiUsers.Row

        mnuDeleteRow.Enabled = True

        strItem = dgrdOrders.Item(intSelectedRow, 0)
        intQuantity = dgrdOrders.Item(intSelectedRow, 1)
        lblStatus.Text = intQuantity & " x " & strItem

    End Select

  End Sub
MigrationUser 1  Thursday, May 22, 2003 9:40 PM
In order to be able to highlight the entire row of a selected record in the datagrid, you must create a DataGridTextBoxColumn class that inherits from the System.Windows.Forms.DataGridTextBoxColumn class.

The Paint method is responsible for highlighting the selected row.

Note that there is also a DataGridTableStyle class that provides the formatting of the DataGrid.

Below is the C# code from the Taskvision DataGridTextBoxColumn class.

Greg

---------------------------------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace TaskVision
{
public class DataGridTextBoxColumn : System.Windows.Forms.DataGridTextBoxColumn
{
private bool m_IsRedIfOverDue = false;

public DataGridTextBoxColumn(string format, string headerText, string mappingName, int width)
{
base.Format = format;
base.HeaderText = headerText;
base.MappingName = mappingName;
base.Width = width;
}

public DataGridTextBoxColumn(string format, string headerText, string mappingName,
int width, bool isRedIfOverDue)
{
base.Format = format;
base.HeaderText = headerText;
base.MappingName = mappingName;
base.Width = width;
m_IsRedIfOverDue = isRedIfOverDue;
}

protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, 
System.Drawing.Rectangle bounds, bool isReadOnly, string instantText, bool cellIsVisible)
{
// Do Nothing
// This is a ReadOnly DataGrid
}

protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, 
System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, 
System.Drawing.Brush foreBrush, bool alignToRight)
{
object bVal = GetColumnValueAtRow(source, rowNum);

if (this.Format == "d")
{
try
{
// globalize for german
if (Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName == "de")
bVal = String.Format("{0:d}", Convert.ToDateTime(bVal).ToString("dd/MM/yy"));
else
bVal = String.Format("{0:d}", Convert.ToDateTime(bVal));
}
catch
{
// ignore an invalid cast
}
}

// if the current row is this row, draw the selection back color
if (this.DataGridTableStyle.DataGrid.CurrentRowIndex == rowNum)
{
g.FillRectangle(new SolidBrush(this.DataGridTableStyle.SelectionBackColor), bounds);
g.DrawString(Convert.ToString(bVal), this.DataGridTableStyle.DataGrid.Font,
new SolidBrush(this.DataGridTableStyle.SelectionForeColor), bounds.X + 2, bounds.Y + 2);
}
else
{
g.FillRectangle(backBrush, bounds);

if (m_IsRedIfOverDue)
{
try
{
if (Convert.ToDateTime(bVal).Date < DateTime.Now.Date)
g.DrawString(Convert.ToString(bVal), this.DataGridTableStyle.DataGrid.Font,
new SolidBrush(Color.Red), bounds.X + 2, bounds.Y + 2);
else
g.DrawString(Convert.ToString(bVal), this.DataGridTableStyle.DataGrid.Font, foreBrush,
bounds.X + 2, bounds.Y + 2);
}
catch
{
g.DrawString(Convert.ToString(bVal), this.DataGridTableStyle.DataGrid.Font, foreBrush,
bounds.X + 2, bounds.Y + 2);
}
}
else
{
g.DrawString(Convert.ToString(bVal), this.DataGridTableStyle.DataGrid.Font, foreBrush,
bounds.X + 2, bounds.Y + 2);
}
}
}
}
}
MigrationUser 1  Friday, May 23, 2003 9:32 AM
Shame on Microsoft, where there designers doped ?
Why cant they have a FullRowSelect = true/false property like in ListView.
And why can't  those dumb asses provide datasource property to populate Rows and Columns  given a table or DataView just like Datagrid but for the ListView.

Shame on Crack pots in Microsoft.
MigrationUser 1  Saturday, December 18, 2004 4:32 PM
Use Datagrid.Select(RowIndex)

this will select teh full row
MigrationUser 1  Friday, April 01, 2005 11:45 AM

I'm stuck on .NET 1.1 for the time being, thanks for the example, buthonestly, I can't figure out how to attach this to my DataGrid. Can you expand on the example a little more?

Thanks,

Scott

Scott Glass  Friday, February 17, 2006 3:25 PM

You can use google to search for other answers

Custom Search

More Threads

• Web service security
• Remote access performance
• three tier archetecture
• TaskVision Server source.
• Unstable terrarium
• Download link for Windows Samples?
• Is it possible to use the PhotoListView outside of this application?
• Ver 1.1/Terrarium Server is not working 'n What about the contest rules?
• Puzzled
• User control problem