Solved

GridView Control

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ Select all records by MAX data syntax help 1 46
VB.NET - Refactor Class per SOLID principles 2 39
parse string in c# 5 40
GET INFO ABOUT WHAT THE PRINTER IS DOING ? 11 69
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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