Solved

DataGrid <HeaderTemplate> question

Posted on 2004-03-22
10
561 Views
Last Modified: 2008-01-09
Hi guys,

I have a datagrid with a few columns, everything working fine, when I added the code below I start running into trouble.

I want to use the header row as my datagrid filter, thus when the user enter a value in Textbox2 (Drawing Number Header) and click my search button I want to run sql with a WHERE clause that includes only that drawing number.  However it seems like Textbox2 just is NOT RECOGNIZED in my codebehind page.


<asp:TemplateColumn HeaderText="DrawingNumber">
      <HeaderTemplate>
            <asp:TextBox id="Textbox2" runat="server" Text="enter drawing number"></asp:TextBox>
      </HeaderTemplate>
      <ItemTemplate>
            <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DrawingNumber") %>'>
            </asp:Label>
      </ItemTemplate>
      <EditItemTemplate>
            <asp:TextBox id=TextBox1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DrawingNumber")             %>'></asp:TextBox>
      </EditItemTemplate>
</asp:TemplateColumn>



My code run into my catch block when I set my sql string, below is a listing of my code-behind for further reference:



    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        PopDataGrid1()
    End Sub

    Function PopDataGrid1()
        Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("connStr"))
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dr As SqlDataReader = Nothing

        Try
            conn.Open()
            Dim sql As String

            sql = "SELECT PartNumber, PartDescription, DrawingNumber, ChangeLevel, ParentDrawing, " & _
                    " DrawingLocation, DrawingOrigin, StatusCode, UsageFlag, PPSNumber, PCONumber," & _
                    " EffectivePoint, WFUO, StockDisposition, DesignResponsibility, EngineerCode, UserID " & _
                    " FROM tblItemMaster WHERE DrawingNumber = '" & TextBox2.Text.ToString() & "' ORDER BY PartNumber"
            Response.Write("clicked search btn - 4")

            Dim rs As New SqlCommand(sql, conn)
            rs.CommandType = CommandType.Text
            da.SelectCommand = rs

            da.Fill(ds)
            DataGrid1.DataSource = ds
            DataGrid1.DataBind()

            If DataGrid1.Items.Count = 0 Then
                DataGrid1.Visible = False
            Else
                DataGrid1.Visible = True
            End If
        Catch
            Response.Write("catch blocked entered...")
        Finally
            If Not conn Is Nothing Then
                conn.Close()
                conn.Dispose()
                conn = Nothing
            End If
            If Not SqlDataAdapter1 Is Nothing Then
                da.Dispose()
                da = Nothing
            End If
        End Try
    End Function
0
Comment
Question by:africaoh
  • 5
  • 3
  • 2
10 Comments
 
LVL 7

Expert Comment

by:dante469
ID: 10649464
Since TextBox2 is part of the datagrid believe you will need to ref as follows:

CType(e.Item.FindControl("TextBox2"), textbox).Text


   sql = "SELECT PartNumber, PartDescription, DrawingNumber, ChangeLevel, ParentDrawing, " & _
                    " DrawingLocation, DrawingOrigin, StatusCode, UsageFlag, PPSNumber, PCONumber," & _
                    " EffectivePoint, WFUO, StockDisposition, DesignResponsibility, EngineerCode, UserID " & _
                    " FROM tblItemMaster WHERE DrawingNumber = '" & CType(e.Item.FindControl("TextBox2"), textbox).Text  & "' ORDER BY PartNumber"

BTW...  Why 125 points???

Have Fun,
Dante
0
 

Author Comment

by:africaoh
ID: 10649538
Hi Dante,

Makes sense, however my function is called:
Function PopDataGrid1()

The above function is called on the button clicked event that is:

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        PopDataGrid1()
End Sub


I would need a function with something like:
(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand

How would I go about getting "e.Item" to work in my function ??

Please help.
0
 
LVL 7

Expert Comment

by:dante469
ID: 10650261
Good question....

Typically e.item points to the selected item in the dg...  Since TextBox2 is part of the header and not a repeater may be able to access as follows:


CType(dg1.FindControl("TextBox2"), TextBox).Text  'This assumes dg1 is the name of your datagrid...

Please let me know if any issues...

Have Fun,
Dante


0
 

Author Comment

by:africaoh
ID: 10650392
Hi Dante,

Good idea, but it runs into my catch block, when I saw your code: dg1.FinControl("id") I thought off course that would work, but it didnt..

If you have any other ideas how to pickup that Textbox2 in my header please let me know.

Thank you.

Guess this question is worst 125p after all ;)
0
 
LVL 4

Accepted Solution

by:
locitt earned 125 total points
ID: 10654004
Dim txt As TextBox = IIf(AllowPaging,  CType(CType(CType(DataGrid1.Controls(0), Table).Rows(1), DataGridItem).FindControl("TextBox2"), TextBox), CType(CType(CType(DataGrid1.Controls(0), Table).Rows(0), DataGridItem).FindControl("TextBox2"), TextBox))

I assumed that the TemplateColumn is the first column with 0 index.

HTH.
locitt.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:africaoh
ID: 10657619
locitt,

Worked 100%, thanks alot!, btw did you just come up with that?, do you have some or other really good book your using or where do you get the solutions?

Thanks again!
0
 
LVL 4

Expert Comment

by:locitt
ID: 10663256
I spent 2 months to make a custom control inherited from MS DataGrid control, so I know it well. You can access to many sources about DataGrid control by search engine. Thanks for the points.

locitt.
0
 

Author Comment

by:africaoh
ID: 10663301
That's what im trying to do now with my datagrid (custom control), have to pack alot of functionality into the grid, including  header filters, scrollable, fixed headers, dynamic columns, sorting, paging, edit, ect.

Just need some advise on the following from you:
1. My tables that my dg queries has about 30000 records, what would be the most effective manner to get the data from the db, I thought about a disconnected dataset, have you worked with that some?  Or I have to cut back on my viewstate controls, at the moment it is quite slow.

2. Also I want to display a form over my dg when the user click on the row in order to update the row, dont want to do it in the record itself, here's an example:
http://www.infragistics.com/products/grids.asp (see second image from the top)

Hope to hear from you.


0
 
LVL 4

Expert Comment

by:locitt
ID: 10663413
wow, you try the same thing i've made. until now, i've got some of the features but not all.

>> 1. My tables that my dg queries has about 30000 records, what would be the most effective manner to get the data from the db, I thought about a disconnected dataset, have you worked with that some?  Or I have to cut back on my viewstate controls, at the moment it is quite slow.

I use disconnect dataset.
at the design time, it might need viewstate to store the dataset but runtime, i did with session object and unique key by GUID stored in viewstate.

with 3000 records, and multiple-editing mode (all are editable, with textboxes, dropdown,...controls), it's really heavy load.  But single editing mode is fine.

===> 2. Also I want to display a form over my dg when the user click on the row in order to update the row, dont want to do it in the record itself, here's an example:

if so, i will make a hidden div with all the controls needed for every controls in datagrid's columns. Then show the div when user clicks on the arrow button. And also, i think you need to hide all intersect SELECT element cause it will overlap your div.

here are some scripts those you might need:


function fx_elementTop(eSrc)
{
      var iTop = 0;
      var eParent;
      eParent = eSrc;
      while (eParent != null && eParent.tagName.toUpperCase() != "BODY")
      {
            iTop += eParent.offsetTop;

            if(eParent.scrollTop != null)
                  iTop -= eParent.scrollTop;

            eParent = eParent.offsetParent;
            if (eParent == null)
                  break;
      }
      return iTop;
}

function fx_elementLeft(eSrc)
{      
      var iLeft = 0;
      var eParent;
      eParent = eSrc;
      while (eParent  != null&& eParent.tagName.toUpperCase() != "BODY")
      {
            iLeft += eParent.offsetLeft;
            if(eParent.scrollLeft != null)
                  iLeft -= eParent.scrollLeft;
            eParent = eParent.offsetParent;
            if (eParent == null)
                  break;
      }
      return iLeft;
}


function fx_hideItsElement(elmID, eHost)
{
      var obj;

      // Hide any element that overlaps with the control eHost
      for (i = 0; i < fx_getTags(elmID).length; i++)
      {
            obj = fx_getTags(elmID)[i];
            if (fx_elementTop(obj) > parseInt(eHost.style.top) + eHost.offsetHeight)
            {
                  //if element is below bottom of control then do nothing
            }
            else if (fx_elementLeft(obj) > parseInt(eHost.style.left) + eHost.offsetWidth)
            {
                  //if element is to the right of control then do nothing
            }
            else if (fx_elementLeft(obj) + obj.offsetWidth < parseInt(eHost.style.left))
            {
                  //if element is to the left of control then do nothing
            }
            else if (fx_elementTop(obj) + obj.offsetHeight < parseInt(eHost.style.top))
            {
                  //if element is to the top of control then do nothing
            }
            else
            {
                  var n = obj.id == undefined ? obj.name : obj.id;
                  var o = fx_getChild(eHost, n);

                  if (o == null)
                  {
                        obj.style.visibility = "hidden";
                        hiddens[hiddenCount] = obj.uniqueID;
                        hiddenCount = hiddenCount + 1;
                  }
            }
      }
}

function fx_showItsElement()
{
      for (var i=0;i<hiddenCount;i++ )
      {
            var obj = fx_getById(hiddens[i]);
            if (obj != null)
            {
                  obj.style.visibility = "visible";
            }
      }

      hiddens = new Array();
      hiddenCount = 0;
}

function fx_clickInElement(elmID, x, y)
{
      var obj = fx_getById(elmID);
      var eleTop = fx_elementTop(obj);
      var eleLeft = fx_elementLeft(obj);
      var eleHeight = obj.offsetHeight;
      var eleWidth = obj.offsetWidth;
      return ((y >= eleTop) && (y <= (eleTop + eleHeight)) && (x >= eleLeft) && (x <= (eleLeft + eleWidth)));
}

locitt.
0
 

Author Comment

by:africaoh
ID: 10663456
locitt,

thanks for the scripts i will be sure to look at them, can you send me an email to upe79@hotmail.com, then we exchange ideas ect, also my grid sits on the intranet at the moment will move it to an internet host so you can have a look within a week or so from today.

let me know, it's good to know there is someone trying to do the same thing at the moment.

cheers
Cornelius

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.NET NamedPipe access using ip address 5 53
Problem to Popup 2 51
Check if number is currency 15 29
Format column on datatable 7 17
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now