Solved

GridView Control

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

19 Experts available now in Live!

Get 1:1 Help Now