Solved

GridView Control

Posted on 2006-11-09
5
284 Views
Last Modified: 2012-05-05
hi all... i am using this section of code to capture a row cell that is clicked by the user...

 public void DataGridID_ItemDataBound(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        int l;
        int index = Convert.ToInt32(e.CommandArgument);
        // Retrieve the row that contains the button clicked by the user from the Rows collection.
        GridViewRow row = GridView1.Rows[index];
        l = int.Parse(row.Cells[2].Text);       //Get id

....
    }

the problem is that when i add sorting and paging to the the grid via the "smart tag" the following line "int index = Convert.ToInt32(e.CommandArgument);" catches the column value which is a string and errors out.  What is the correct way to go about paging and sorting my grid using the following code i have posted.  
0
Comment
Question by:joan2006
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:kraffay
ID: 17910889
I suggest isntead using:

  int index = e.Item.ItemIndex;


0
 
LVL 1

Expert Comment

by:riyaz404
ID: 17911895
You might have already done this, but if not...

Do you use the DataSourceID instead of DataSource to bind?
If you use DataSourceID to bind, then you will not have to worry about paging or sorting events. They dont need to be handled..You cannot assign a regular DataSet or DataView to DataSourceID

You need to assign, SQLDataSource or ObjectDataSource..SQLDataSource can be used to bind a query written in Oracle, SQL Server, or any other Database. Its not like your SqlDataAdaptore or OracleDataAdapter in framework 1.1
0
 

Author Comment

by:joan2006
ID: 17979875
kraffay ,

There is not an option for e.Item.ItemIndex;
 only CommandArgument, CommandName, CommandSource, Equals, GetHashCode, GetType and ToString ...


Any more suggestions
0
 

Author Comment

by:joan2006
ID: 17981857
riyaz404,

Help me with the paging ...you say if i use DatasourceID then i dont have to code for paging..

i get an error:

Error      1      Cannot implicitly convert type 'System.Data.DataSet' to 'string'      

this is how i am using it... please help me figure this out..
               
             DataSet ds = GetAllWFXLSFiles();

                if (ds.Tables[0].Rows.Count > 0)
                {
                   // GridView1.DataSource = ds;
                    GridView1.DataSourceID = ds;  //try datasourceid instead
                    GridView1.DataBind();  org
                    //GridView1.DataSource = ds.Tables[0].DefaultView;
                    ds.Dispose();
                }

0
 
LVL 1

Accepted Solution

by:
riyaz404 earned 500 total points
ID: 17982039
to use dataSourceID, you need to you use the new datasourcecontrols of .net 2.0.

Build a datasource control, by dragging one from toolbox. Once you have that, you can use the name of that datasourcecontrol to assign to the datasourceid.

You cannot assign a dataset as a datasoureid. It has to be a classname that is of type DataSourceControls.

Like this -
(following goes in the aspx file)

<asp:SqlDataSource ID="CustomersList" Runat="server"
SelectCommand="SELECT CompanyName, ContactName, ContactTitle, City FROM Customers"
ConnectionString=
"Data Source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=Northwind">
</asp:SqlDataSource>

<asp:GridView id="MyDataGrid" DataSourceID="CustomersList" runat="server"/>

Or in your code (cs file) -
MyDataGrid.DataSourceID = "CustomersList";


0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

761 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