We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

[4676] Building a Full-Featured Custom DataGrid Control

Last post 10-09-2006 1:32 PM by adwivedi1. 31 replies.
Page 2 of 3 (32 items) < Previous 1 2 3 Next >
Sort Posts: Previous Next
  • 06-02-2005 8:20 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45

    Setting DataGrid column width at run time

    Sure, just add your datagrid column modifications in the ItemCreated event handler.

    Ex. in Pixels:
    Code:
    'VB
    Private Sub GridCreated (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles GridCreated
      e.Item.Cells(0).Width = New Unit (100, UnitType.Pixel)
      e.Item.Cells(1).Width = New Unit (75, UnitType.Pixel)
    End Sub

    //C#
    private void GridCreated (object sender, DataGridItemEventArgs e)
    {
      e.Item.Cells[0].Width = new Unit (100, UnitType.Pixel);
      e.Item.Cells[1].Width = new Unit (75, UnitType.Pixel);
    }

    Furthermore, you can accomodate other Unit structures such as Percentage.

    Ex. in Percentage:
    Code:
    'VB
    Private Sub GridCreated (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles GridCreated
      e.Item.Cells(0).Width = New Unit (50, UnitType.Percentage)
      e.Item.Cells(1).Width = New Unit (25, UnitType.Percentage)
       End Sub

    //C#
    private void GridCreated (object sender, DataGridItemEventArgs e)
    {
      e.Item.Cells[0].Width = new Unit (50, UnitType.Percentage);
      e.Item.Cells[1].Width = new Unit (25, UnitType.Percentage);
    }


    Hope this helps.

    - Jimmy Markatos
    • Post Points: 0
  • 06-02-2005 8:23 PM In reply to

    • anabhra
    • Not Ranked
    • Joined on 06-02-2005
    • New Member
    • Points 25
    thanks a lot.

    your suggestion will work.

    I just solved my problem by setting the header template and itemtemplate widths. I had the grid in a div and that is why I could not see the widths. Once I set the table layout of the grid to fixed, I saw the columns being sized.

    again, many thanks for your interest.
    • Post Points: 0
  • 06-02-2005 8:26 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45
    You're welcome. Glad to help!
    • Post Points: 0
  • 06-06-2005 4:06 PM In reply to

    • anabhra
    • Not Ranked
    • Joined on 06-02-2005
    • New Member
    • Points 25
    I have one more question:

    When using template columns dynamically, are they stored in the view state?
    It seems not but I would like to have them as I have editable text boxes in the template columns that user enters data in...

    Many thanks,
    anabhra
    • Post Points: 0
  • 06-06-2005 4:59 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45
    Yes, becuase by default all data within a DataSet is typically persisted in ViewState. Have you explcitly disabled Page ViewState? I don't see how your dynamically sized columns would reset themselves.

    At any rate, try enabling the DataGrid's DataGrid.EnableViewState property to true to maintains its state across HTTP requests.

    Furthermore, you can ideally stored any values you require to be persistant by storing those values in Session State.

    This should take care of it.

    - Jimmy Markatos
    • Post Points: 0
  • 06-06-2005 5:04 PM In reply to

    • anabhra
    • Not Ranked
    • Joined on 06-02-2005
    • New Member
    • Points 25
    I read this article on msdn:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridcolumncollectionclasstopic.asp

    It seems to say that derived coumns (from say itemtemplate) that are added at run time are not saved to view state.

    They suggest having all needed cols and set visible attribute of those not needed for display to be set to false.

    I do have the EnableViewState=true (I think that is the default) on the page and grid.  I have 2 other grids that do maintain the state...

    -anabhra
    • Post Points: 0
  • 06-06-2005 7:07 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45
    Right, I misunderstood you, as I was referring to DataSet data and I simply specified if you explicitly disabled view state, as yes, of course, true is the default. Now if you utilize what else I've mentioned then you shouldn’t have any issues.

    Also, I don't know what you mean with 2 other grids that maintain state. Are they all the same with dynamic item templates and all? As I can’t see your code I don’t know what you may have overlooked or otherwise.

    Therefore, employ the Page_Init method to help with this, but ultimately whenever something doesn't hold state by normal means as you would’ve hope it would then make use of view state.

    Hope this helps.
    • Post Points: 0
  • 06-13-2005 3:37 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45

    Custom DG Ctrl w/Dual Paging, Drag&Drop/Sort Code

    Hi all,

    I just re-added the forum post containing the new version of my article code. I just thought it easier to repost it again, since I find people requesting this and since the topics become hidden after a while, it won't show up unless you select all topics.

    Anyway, here is the link:

    Custom DG Ctrl w/ Dual Paging, Drag&Drop/Sort DL code forum post

    -Jimmy Markatos
    • Post Points: 0
  • 06-23-2005 10:19 PM In reply to

    • enigma55
    • Not Ranked
    • Joined on 06-23-2005
    • New Member
    • Points 5
    I'm having the same problem.  Please let me know if you find anything.
    • Post Points: 0
  • 07-14-2005 4:53 AM In reply to

    sorting function

    how can i implement sorting for all the column in this sample?
    • Post Points: 0
  • 07-15-2005 4:25 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45
    Hi,

    You can implement one way as found in my other article here on DF - Dynamic Column Sorting and Paging in ASP.NET or you can download the upgraded version of my custom datagrid control with drag and drop columns and sorting.

    -Jimmy Markatos
    • Post Points: 0
  • 09-05-2005 9:32 AM In reply to

    • Rujuta
    • Not Ranked
    • Joined on 09-30-2002
    • New Member
    • Points 30

    ITemplate Checkbox event not fired

    I am having a datagrid where i am binding all boundcolumns from dataset and along with it i am having 2 templates , one is label and another checkbox . these two controls are bound to datagrid using itemplate class (i.e. dynamically) . when i am attaching checkbox and allocating event checkedchanged , its not getting fired and also i am unable to get how many checkbox are clicked (if autopostback=false) . if autopostback is true then datagrid is not displayed but if i again bind datagrid with all bouncontrols usinf sessions then i get same old datagrid but all checkbox are false .

    I hope i am clear with my problem . please help me in getting event fired for checkbox and also getting count for number of check box clicked .

    Thanks in advance,
    Rujuta
    • Post Points: 0
  • 10-31-2005 6:32 AM In reply to

    • sneha123
    • Not Ranked
    • Joined on 10-31-2005
    • New Member
    • Points 25

    selecting multiple items in a checkbox from a dat

    There will be some 20 questions and  for each question there will be  4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net
    pls help me

    we have written the code using radio button for selecting single item.but we  want to replace it with checkbox to select multiple items. the code using radio button is given below .pls correct it with checkbox

    Imports System.Data
    Imports System.Data.SqlClient
    Imports ELearning.LAIDBC
    Public Class Test
      Inherits System.Web.UI.Page
      Public ds As New DataSet()
      Public ds1 As New DataSet()
      Public ds2 As New DataSet()
      Public Score, Answered As Integer
      Protected WithEvents txtNoOfqns As System.Web.UI.WebControls.TextBox
    #Region " Web Form Designer Generated Code "

      'This call is required by the Web Form Designer.
      <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

      End Sub

      Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
          'CODEGEN: This method call is required by the Web Form Designer
          'Do not modify it using the code editor.
          InitializeComponent()
      End Sub
      Protected WithEvents lblCourse As System.Web.UI.WebControls.Label
      Protected WithEvents lblCourseName As System.Web.UI.WebControls.Label
      Protected WithEvents lblNoOfQuestions As System.Web.UI.WebControls.Label
      Protected WithEvents lblDurationInMinutes As System.Web.UI.WebControls.Label
      Protected WithEvents txtduration As System.Web.UI.WebControls.TextBox
      Protected WithEvents lblTimeLeft As System.Web.UI.WebControls.Label
      Protected WithEvents lblAnswered As System.Web.UI.WebControls.Label
      Protected WithEvents txtanswered As System.Web.UI.WebControls.TextBox
      Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
      Protected WithEvents submitbtn As System.Web.UI.WebControls.Button
      Protected WithEvents lab2 As System.Web.UI.WebControls.Label
      Protected WithEvents Lab4 As System.Web.UI.WebControls.Label
      Protected WithEvents Label2 As System.Web.UI.WebControls.Label
      Protected WithEvents lab1 As System.Web.UI.WebControls.Label
      Protected WithEvents txtans As System.Web.UI.WebControls.TextBox

    #End Region
      Private ConDB As New ELearning.LAIDBC()
      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          If Session("Log") = Nothing Then
              Response.Redirect("frmLogin.aspx")
          End If

          If Not IsPostBack Then

              Try
                  Session("Course") = Session("URL2")
                  Session("Id") = "reshm-001"
                  Session("CandId") = "00000001"
                  'Response.Write(Session("Id"))
                  'Response.End()
                  Session("TotScore") = Nothing
                  Dim count1, str11 As String
                  ds1 = New DataSet()
                  ConDB.OpenConnection()

                  ds1 = ConDB.ExecuteSPReturnDS("ELS_ExecuteQuery", "select count(f_UserId) from tbl_Score where f_UserId='" & (Session("CandId")) & "'  and  f_courseid='" & Session("Id") & "' ")
                  ConDB.CloseConnection()
                  count1 = ds1.Tables(0).Rows(0)(0)
                  str11 = "select count(f_UserId) from tbl_Score where f_UserId='" & (Session("CandId")) & "'  and  f_courseid='" & Session("Id") & "'"
                  'Response.Write(str11)
                  'Response.End()
                  If count1 < 3 Then
                      ' Response.Write(str11 & "," & count1)
                      ds2 = New DataSet()
                      Dim count2 As Integer
                      ConDB.OpenConnection()
                      ds2 = ConDB.ExecuteSPReturnDS("ELS_ExecuteQuery", "select count(f_UserId) from tbl_Score where f_UserId='" & (Session("CandId")) & "' and f_result='P'")
                      count2 = ds2.Tables(0).Rows(0)(0)
                      Response.Write(count2)
                      ConDB.CloseConnection()
                      If count2 > 0 Then

                          Session("msg") = "You have already passed for the test"
                          Response.Redirect("frmChance.aspx")
                      Else
                          Dim qrstr As String
                          Dim NoOfQuestionsToDisplay As Integer
                          Dim Mstring As String
                          Dim TotalNoOfQuestions As Integer
                          courseidselect()
                          Try
                              NoOfQuestionsToDisplay = txtNoOfqns.Text
                              Session("NoOfQuestionsToDisplay ") = txtNoOfqns.Text
                              ds = New DataSet()
                              ConDB.OpenConnection()
                              ds = ConDB.ExecuteSPReturnDS("ELS_ExecuteQuery", "select  count(*)  from tbl_Question where f_CourseId='" & Session("Id") & "'")
                              ConDB.CloseConnection()
                              TotalNoOfQuestions = ds.Tables(0).Rows(0)(0)
                              Session("TotQns") = TotalNoOfQuestions
                          Catch ex As Exception
                              R
    • Post Points: 0
  • 11-22-2005 11:21 AM In reply to

    unable to update and sort on click

    I have created a Custom control using your article but i am having problem in sorting and update
    as i use a class which implements itemplate interface  for this and create the function for update and
    sorting  in same class.when i click on them the changes are made in database and dataset but they does not display on page and display on second click as these function are called in last i.e the getdatagrid() method is not called after them hence no changes are viewed.Please help me to resolve this problem.Can i use ipostbackdatahandler.

    Thanks in Advance
    • Post Points: 0
  • 12-22-2005 3:23 PM In reply to

    • wrocca
    • Not Ranked
    • Joined on 12-22-2005
    • New Member
    • Points 5
    Quote:
    [1]Posted by taxiturner on 3 May 2005 10:25 PM[/1]
    LiteralControl lc = (LiteralControl) sender;
    DataGridItem container = (DataGridItem) lc.NamingContainer;
    lc.Text = ((DataRowView) container.DataItem) [columnName].ToString();



    I believe that "container" is your problem.
    try:

    lc.Text = ((DataRowView)e.Item.DataItem)[columnName].ToString();


    Now, if someone could just tell me how to cast a DataGridItemCollection as a TableRowCollection so that I don't get this error, that would be helpful.
    • Post Points: 0
Page 2 of 3 (32 items) < Previous 1 2 3 Next >