DataGridView.DisplayedRowCount

Hi,

In my last project i want to calculate only those rows who display in grid not all.

when i am using MyGrid.RowCount or MyGrid.Rows.Count it gives me all rows..

Finally i find event DisplayedRowCount

Here is sample

public int DisplayedRowCount(
bool includePartialRow
)
Parameter : includePartialRow
true to include partial rows in the displayed row count; otherwise, false.

Int32 count = DisplayedRowCount(true);
Int32 count1 = DisplayedRowCount(false);

count return all row count
count1 return all visible row count..

read more at

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.displayedrowcount(v=VS.100).aspx

the order of columns in the DataGridView

In order to change the order of columns, just set the DisplayIndex property of the DataGridView to the desired value. Remember that the index is zero based.

C#
private void btnReorder_Click(object sender, EventArgs e)
{
dgv.Columns["CustomerID"].DisplayIndex = 5;
dgv.Columns["OrderID"].DisplayIndex = 3;
dgv.Columns["EmployeeID"].DisplayIndex = 1;
dgv.Columns["OrderDate"].DisplayIndex = 2;
dgv.Columns["Freight"].DisplayIndex = 6;
dgv.Columns["ShipCountry"].DisplayIndex = 0;
dgv.Columns["ShipName"].DisplayIndex = 4;
}

VB.NET
Private Sub btnReorder_Click(ByVal sender As Object, ByVal e As EventArgs)
dgv.Columns("CustomerID").DisplayIndex = 5
dgv.Columns("OrderID").DisplayIndex = 3
dgv.Columns("EmployeeID").DisplayIndex = 1
dgv.Columns("OrderDate").DisplayIndex = 2
dgv.Columns("Freight").DisplayIndex = 6
dgv.Columns("ShipCountry").DisplayIndex = 0
dgv.Columns("ShipName").DisplayIndex = 4
End Sub

Tab key work as an Enter key in Datagridview

To make the Tab key work as Enter key, we have to inherit DataGridView controls and override two methods ProcessDataGridViewKey and ProcessDialogKey. In both the methods if the user has click Tab then we have to ProcessEnterKey of Datagridview.

VB Code

Public Class NewDataGridView
Inherits DataGridViewProtected Overrides Function ProcessDataGridViewKey(ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
If e.KeyCode = Keys.Tab Then
Me.ProcessEnterKey(e.KeyData)
Return True
End If
Return MyBase.ProcessDataGridViewKey(e)
End Function


Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Tab Then
Me.ProcessEnterKey(keyData)
Return True
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
End Class

C# Code
protected override bool ProcessDataGridViewKey(System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
this.ProcessEnterKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}


protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Tab)
{
this.ProcessEnterKey(keyData);
}return base.ProcessDialogKey(keyData);
}

Tab key work as an Enter key in Datagridview

Hi, this code at keydown and add blank row when you at last row and last column

private void DataGridView1_KeyDown(object sender, KeyEventArgs e)
{
//check for enter key
if (e.KeyCode == Keys.Enter)
{
DataGridView vw = (DataGridView)sender;
//check for last row
if (vw.CurrentCell.RowIndex == vw.Rows.Count - 1)
{
//if it's not the last cell, move to the next one
if (vw.CurrentCell.ColumnIndex != vw.ColumnCount - 1)
{
vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex].Cells[vw.CurrentCell.ColumnIndex + 1];
}
else // if last cell then either enter new raw or move to first cell.
{
//move to first column in new row
vw.Rows.Add(1);
vw.Refresh();
SendKeys.Send("{HOME}");
}
}
else
{
//if it's the last cell, move to the next row, first cell
if (vw.CurrentCell.ColumnIndex == vw.ColumnCount - 1)
{
vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex + 1].Cells[0];
}
else
{
//move to the next cell on the current row
vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex].Cells[vw.CurrentCell.ColumnIndex + 1];
}
}
//mark the keycode as handled
e.Handled = true;
}
}

%d bloggers like this: