IT Community - Software Programming, Web Development and Technical Support

C#.Net, ASP.Net Data Grid Frequently Asked Questions

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.....


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #41 (permalink)  
Old 02-25-2008, 02:09 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default How do I programmatically determine the selected rows in a datagrid?

Hi ..

How do I programmatically determine the selected rows in a datagrid?

Thanks..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #42 (permalink)  
Old 02-25-2008, 02:10 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Default Re: C#.Net, ASP.Net Data Grid Frequently Asked Questions

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #43 (permalink)  
Old 02-25-2008, 02:10 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default Re: C#.Net, ASP.Net Data Grid Frequently Asked Questions

Hi..


it would be helpful if you provide the source code for this example..

thanks..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #44 (permalink)  
Old 02-25-2008, 02:11 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Default Re: C#.Net, ASP.Net Data Grid Frequently Asked Questions

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #45 (permalink)  
Old 02-25-2008, 02:12 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default How can I prevent my user from sizing columns in my datagrid?

Hi..

How can I prevent my user from sizing columns in my datagrid?

thanks..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #46 (permalink)  
Old 02-25-2008, 02:14 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Default Re: C#.Net, ASP.Net Data Grid Frequently Asked Questions

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);
     }
}
The above code prevents the sizing cursor from appearing, but as Stephen Muecke pointed out to us, if the user just clicks on the border, he can still size the column. Stephen's solution to this problem is to add similar code in an override of OnMouseDown.

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #47 (permalink)  
Old 02-25-2008, 02:15 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default Re: C#.Net, ASP.Net Data Grid Frequently Asked Questions

Hi..

Send me VB. Net code If you have..

thanks..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #48 (permalink)  
Old 02-25-2008, 02:16 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Default Re: C#.Net, ASP.Net Data Grid Frequently Asked Questions

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #49 (permalink)  
Old 02-27-2008, 08:42 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default How can I detect when a cell starts being edited, not when it becomes current?

hi..

How can I detect when a cell starts being edited, not when it becomes current?

thanks..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #50 (permalink)  
Old 02-27-2008, 08:43 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Default Re: C#.Net, ASP.Net Data Grid Frequently Asked Questions

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #51 (permalink)  
Old 02-27-2008, 08:43 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default In my datagrid, if I press tab to enter a column using a derived columnstyle,?

Hi..

In my datagrid, if I press tab to enter a column using a derived columnstyle, the column does not receive focus. Why?

thanks..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #52 (permalink)  
Old 02-27-2008, 08:49 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Default Re: C#.Net, ASP.Net Data Grid Frequently Asked Questions

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 05:15 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0