This is a discussion on C#.Net, ASP.Net Data Grid Frequently Asked Questions within the C# Programming forums, part of the Software Development category; Hi .. How do I programmatically determine the selected rows in a datagrid? Thanks.....
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi.. If you have added a TableStyle for your grid, then the code below should set the right column width to be the empty space from a button click. If you need to dynamically do this in response to the user sizing other columns, then there may be more work. But if you only need to do it at the end of your Form_Load, then this code might be sufficient. It assumes your datasource is a datatable. thanks..
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| HI.. Code: private void button1_Click(object sender, System.EventArgs e)
{
int numCols = ((DataTable)(dataGrid1.DataSource)).Columns.Count;
//the fudge -4 is for the grid borders
int targetWidth = dataGrid1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth - 4;
int runningWidthUsed = this.dataGrid1.TableStyles["customers"].RowHeaderWidth;
for(int i = 0; i < numCols - 1; ++i)
runningWidthUsed += this.dataGrid1.TableStyles["customers"].GridColumnStyles[i].Width;
if(runningWidthUsed < targetWidth)
this.dataGrid1.TableStyles["customers"].GridColumnStyles[numCols - 1].Width = targetWidth - runningWidthUsed;
}
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| You can do this by subclassing your grid and overriding OnMouseMove, and not calling the baseclass if the point is on the columnsizing border. Code: public class MyDataGrid : DataGrid
{
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y));
if(hti.Type == DataGrid.HitTestType.ColumnResize)
{
return; //no baseclass call
}
base.OnMouseMove(e);
}
} Code: protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y));
if(hti.Type == DataGrid.HitTestType.ColumnResize)
{
return; //no baseclass call
}
base.OnMouseDown(e);
}
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| Hi.. Here is the vb.Net Code. Code: Public Class MyDataGrid
Inherits DataGrid
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
If hti.Type = DataGrid.HitTestType.ColumnResize Then
Return 'no baseclass call
End If
MyBase.OnMouseMove(e)
End Sub
End Class Code: Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
If hti.Type = DataGrid.HitTestType.ColumnResize Then
Return 'no baseclass call
End If
MyBase.OnMouseDown(e)
End Sub
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| Hi.. You can use the CurrentCellChanged event to detect when the currentcell changes position. But this event will not allow you to catch the start of a cell being edited. One way you can do this is to catch the TextChanged event in the embedded TextBox within the cell. If the text changes, then it might be the beginning of a cell edit provided the text in the TextBox differs from the stored value from the DataSource. The reason you need to check for a different value between the grid DataSource and the TextBox contents is that the TextChanged event is fired initially when the TextBox is initialized when the cell becomes current and moves the value from the grid ataSource to the TextBox. You also have to ignore subsequent hits of TextChanged as the same cell continues to be edited. Here is both a VB and C# sample that implements this strategy to flagged current cell start editing.
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| Hi.. This behavior can be seen when you embedded a control like a textbox or combobox in your derived GridColumnStyle. If you press the tabkey slowly, you may see the cell get focus on the downkey and the cell lose focus on the upkey. One way to avoid this problem is to subclass the embedded control, and override its WndProc method, ignoring the KeyUp Code: public class MyCombo : ComboBox
{
private const int WM_KEYUP = 0x101;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == WM_KEYUP)
{
return; //ignore the keyup
}
base.WndProc(ref m);
}
}
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Paging in Grid | Kirubhananth | ASP and ASP.NET Programming | 8 | 11-28-2008 01:51 AM |
| Edit grid | Kirubhananth | ASP and ASP.NET Programming | 7 | 11-14-2008 05:08 AM |
| .Net3.0 framework Frequently Asked Questions | Mramesh | ASP and ASP.NET Programming | 0 | 03-18-2008 10:07 PM |
| Crystal Reports for VS .NET Frequently Asked Questions | Mramesh | C# Programming | 5 | 03-05-2008 11:15 PM |
| Data grid in PHP | jegan | PHP Programming | 2 | 07-20-2007 07:38 AM |