I need to find and display the number of rows returned by the query. This query is made using an SQLDataSource object, which is bound to an asp.net GridView control. How can I find this information?
You can't use the Rows property on the grid, because that only gives you what the GridView is currently rendering. You need to hook up to the Selected event on the SqlDataSource and then you can pull the AffectedRows property.
protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e) {
int totalRows = e.AffectedRows;
}
I would suggest that you handle the SQLDataSource.Selected event and check the e.AffectedRows property. It returns the number of selected rows. Also, it is possible to obtain this information programmatically:
DataView dv = (DataView)SQLDataSource1.Select(DataSourceSelectArguments.Empty);
int rowCount = dv.Count;
NOTE: this code will result in selecting data once again. So, the best solution is to use the Selected event for this purpose.
Related
Just wanted to know if this is possible with C#.
I have a GridView. The Datasource of the GridView is a DataTable generated from a Database. The AutoGenerateColumn is True.
Therefore when I try the following code
gridView.Columns.RemoveAt(1); //I got 12 Columns from the DataTable
I get the following error:
Exception Details: System.ArgumentOutOfRangeException: Index was out
of range. Must be non-negative and less than the size of the
collection.
I don't want to delete it from the DataTable. I want to delete the column before I use RenderControl to convert the GridView to html text. And I don't want the column to show up in html text. I tried doing this too:
foreach (GridViewRow row in gv.Rows){
row.Cells[1].Visible = false;
}
But it doesn't hide the Column Header.
Anyone have any idea if it is possible to delete column?
Try this
gridView.columns.RemoveAt(1);
gridView.Databind();
Try this: On GridView1.RowDataBound event
GridView1.Columns(0).Visible = False
OR
protected void bla_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false; // hides the first column
}
Actually, RowCreated will be called several times each time the GridView renders. But it works
In my grid-view control 1000 records already there(binded from dataset) when I apply search criteria it shows 500 records.
Now I want to sort only that particular 500 rows when I click on the Grid column name.
MyGrid.Datasource = ds.Tables[0].DefaultView;
MyGrid.DataBind();
Note : Before applying filter(search), I do not want to sort my Grid.
Kindly help me to sort only the result gridview.
Create all the functionallity in your GridView that handles sorting by clicking on the column names OnSorting="MyGrid_Sorting" and AllowSorting="true".
If sorting works correctly set AllowSorting to false.
Now all you have to do is enable the sorting of the GridView in code behind. Preferably in the function that applies the search criteria.
private void applyFilter()
{
MyGrid.AllowSorting = true;
MyGrid.Datasource = ds.Tables[0].DefaultView;
MyGrid.DataBind();
}
I have an empty GridView object on a page that I bind to a LINQ qry result at run time. The GridView has a 'Select" button that fires the SelectedIndexChanged event and it's inside of this event that I'd like to access the data of one of the fields in the selected row.
So far, I can only find one way to do this, and it seems suboptimal:
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView2.SelectedRow;
string UserID = row.Cells[1].Text;
//Do stuff with the userID
}
So this just access the cell data directly based on the cell index. The UserID just happens to be in the second cell and so it works. But later down the road, the UserID may not be in that same column. It seems like I'd be better off looking up the value of this cell by accessing by the cell's header name, or by any method other than the cell index itself.
Any ideas?
Thanks!
Try this
User user = (User)((DataRowView)row.DataBoundItem).Row;
int userID = user.UserID;
I am assuming you have a class called User and you are binding the DataGridView to may a List
I don't know much about asp.net, but in the C# DataGridView you can index columns directly by column name, i.e.
string UserID = row.Cells["UserID"].Text;
or something like that. Might be worth a try.
You can use following code to retrieve object bound to current SelectedRow.
DataTable dt = (DataTable)<GridViewControl>.SelectedRow.DataItem
(here I have assumed that Grid is bound with DataTable.)
Hi I have a grid view which is dynamically generated via C# using a DataSet.
I'm passing a row ID field (from the database) to the grid view, as this is required when a user clicks to edit a records. However I don't want the user to be able to view the column.
I have tried the following but it doesn’t seem to work? Could anyone suggest a way to hide the column or a better way of attaching the information to the grid view row so it can be used at a later stage.
c#
DataColumn ColImplantCustomerDetailsID = new DataColumn();
ColImplantCustomerDetailsID.ColumnName = "Implant ID";
ColImplantCustomerDetailsID.DataType = typeof(int);
ColImplantCustomerDetailsID.Visable = false; <-- visable doens't work here.
asp.net
DataColumn doesn't have a 'Visable' property. Heck, it doesn't have a 'Visible' property either.
Use this before you bind
gridviewNameHere.Columns[index].Visible = false;
You can make it visible again on one of your event handlers.
Another option, rather than hiding the column, is to use the DataKeyNames property of the GridView to store the name of the ID field. You can then use myGridView.SelectedValue to retrieve the selected ID.
Use the index of the column to hide it when you initialize it...
grid.Columns[index].Visible = false;
I actually hide a few things as you are suggesting, example - database status names for that row that can be used in code that the user does not need to see.
Accessing GridView Invisible Columns
http://www.highoncoding.com/Articles/178_Access_GridView_Invisible_Columns.aspx
In the OnRowDataBound event of the GridView component:
// c#
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
int index = 0; // put the index of the column you need hide.
e.Row.Cells[index].Visible = false;
}
I'm creating a dynamic GridView from a DataTable that is returned from a stored procedure. I call and bind with the following code:
DataTable dt = Sql.reportData(Convert.ToInt32(Session["userID"]));
this.GridView1.DataSource = dt.DefaultView;
this.GridView1.DataBind();
I need to restyle certain columns but they are not always the same column number, and only have the headers text string to identify it. Is there an easy way to track a column down like this so I can edit its attributes?
Thanks,
Alex
I've run into this myself. You've got to loop through the column names, get the index, and then refer to the index to manipulate the style.
Muhammad is right about the timing, but you won't be searching for a label--it seems you want to style the entire column, right?
http://forums.asp.net/p/1076872/1584635.aspx
the above has several versions of a solution.
The best place to find the control and use it will be in the RowCreated event. RowDataBound should not be used because you dont have to manipulate the data with which the column is being binded. So restyle the elements in the column by searching them in the RowCreated event.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.FindControl("");
}