datagrid sort custom control question

D

dave

I am building a custom scrollable grid control. I have created buttons in the header that postback and provide to me the column that was clicked on. In most datagrid situations it is up to the programmer to now resort the datasource (dataview) and re-order it before rebinding to the grid control

I am somewhat confused. After a postback my aspx page does not rebind the datagrid with the datasource and it goes right to the custom datagrid control _load procedure. From here it recreates the datagrid. When/where do I integrate the sorting?
It would appear that I would have to capture the column heading click in the aspx page and rebind the grid. Please provide some guidance with this project

th
dav
 
J

Jeffrey Tan[MSFT]

Hi dave,

Thank you for posting in the community!

Based on my understanding, you use button server control in column's
headertemplate, and you want to sort this column when click this button.

======================================================
Yes, just as you said, you should capture the button click event and sort
the data source, then rebind it to the datagrid.

Sample code like this:
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
getsource();
databind();
}
}

public void getsource()
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
ds=new DataSet();
adapter.Fill(ds);
dv=ds.Tables[0].DefaultView;
}

public void databind()
{
DataGrid1.DataSource=dv;
DataGrid1.DataBind();
}

private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Header)
{
DataGridItem dgi=e.Item;
foreach(Control c in dgi.Cells[0].Controls)
{
if(c is System.Web.UI.WebControls.Button)
{
Button bt=(Button)c;
bt.Click+=new EventHandler(bt_Click);
}
}
}
}

private void bt_Click(object sender, EventArgs e)
{
getsource();
dv.Sort="job_id DESC";
databind();
}

I use Sql server's default "jobs" table in "pubs" database to bind to
datagrid

=================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

dave

My event handler for the sorting is within the custom control, however when resorting i will have to databind again the datagrid which is within the page aspx logic.

If a user clicks on a header i can capture that event in the custom control but i need to execute the databind again
i.e.
Dim dv As DataView = dt.DefaultView
dv.Sort = myDataGrid1.SortExpression
With myDataGrid1
.DataSource = dv
.DataBind()
End With

That code is within my aspx page. My problem is, how do i generate the rebind from within the custom control?
thx
 
D

dave

Well i have the problem identified
I can capture the click event in the header of the grid ok. The problem is after a user clicks on the header control (in the datagrid) code in the page (the rebind) is performed before i capture the event of the click (to sort) in the header

After the grid is rebound to the sorted grid the click event in the control is executed but at this point the datasource has already been sorted

How can i ensure that i process the click event of the control header button before the aspx rebinds and sorts the datasource

Currently i capture the click event in the control in the controls load event as follows

Dim s As String = context.Request.Form("__EVENTTARGET"
If InStr(s, "LinkID") > 0 The
Call DataGrid_Sort(s
End I
 
D

dave

The answer to this problem was putting my routine to capture the post back in the myDataGrid_DataBinding event.
 
J

Jeffrey Tan[MSFT]

Hi dave,

Thanks very much for your feedback.

I see your concern, you used customized datagrid control, and you want to
handle the event in the control level not the page level.(I also see that
you prefer to use VB.net, so I will write my sample project in VB.net for
you)

===============================================
I think the implement based on your design of your web application.

If you want to handle the header "sort button" click event in your
customized datagrid control, you can add click event handler for the
button.
I suppose you only use the DataView as the datasource, then in the event
handler, you can get the dataview from the datasource.(cast the datasource
to dataview explicitly)

So you can set the Sort string in your control, then you can re-bind your
datagrid.

Sample code like this:

1).Customized datagrid control's code:
<ToolboxData("<{0}:Customdatagrid runat=server></{0}:Customdatagrid>")>
Public Class Customdatagrid
Inherits System.Web.UI.WebControls.DataGrid

Property SortExpression() As String
Get
Return CType(Me.ViewState("SortExpression"), String)
End Get
Set(ByVal Value As String)
Me.ViewState("SortExpression") = Value
End Set
End Property

Protected Overrides Sub OnItemCreated(ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Header Then
Dim dgi As DataGridItem = e.Item

Dim bt As Button = New Button
bt.ID = "bt"
bt.Text = "SortButton"
AddHandler bt.Click, AddressOf bt_Click

dgi.Cells(0).Controls.Add(bt)
End If
MyBase.OnItemCreated(e)
End Sub

Private Sub bt_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
If (Not Me.DataSource Is Nothing) And (TypeOf (Me.DataSource) Is
System.Data.DataView) Then
Dim dv As DataView = CType(Me.DataSource, DataView)
dv.Sort = Me.SortExpression
Me.DataBind()
End If
End Sub
End Class

2). The Web Page's code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Customdatagrid1.SortExpression = "job_id DESC"
Customdatagrid1.DataSource = getsource()
If (Not IsPostBack) Then
Me.Customdatagrid1.DataBind()
End If
End Sub

Private Function getsource() As DataView
Dim adapter As SqlDataAdapter = New SqlDataAdapter("select * from
jobs", "server=localhost;database=pubs;uid=sa;pwd=")
Dim ds As DataSet = New DataSet
adapter.Fill(ds)
Dim dv As DataView = ds.Tables(0).DefaultView
Return dv
End Function

More: I recommand you expose your header button's click event as its parent
control's(your customized datagrid) event, then you can handle this in the
page level.
This is called Bubble child control event to parent control.

For more information about how to Bubble event in .Net, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconbubblingcommandevent.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconeventbubblingcontrolsample.asp

================================================================
If you still have anything unclear, please feel free to tell me, I will
help you.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi dave,

Thanks very much for your feedback.

I am glad you got what you want. If you have further concern, please feel
free to post, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top