Solved

GridView Control

Posted on 2006-11-09
5
278 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET HttpWebRequest 12 57
New to SOAP and need some direction 10 46
Need to pass stored proc parameters in list 13 33
C# guarantee sql connection close 6 25
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

15 Experts available now in Live!

Get 1:1 Help Now