ASP.NET Gridview case insensitive column sorting - asp.net

I am implementing a gridview to handle a single sql server database table. When I click to sort a column, it does so by capital letters first (e.g. Test, Test2, Test3, test1, test2). Is there a way to manipulate the sorting so that it would return the results as expected (e.g. Test, test1, Test2, test2, Test3)?

Try following in .aspx file:
<asp:GridView ID="grd" runat="server">
</asp:GridView>
And add below in .aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Subject", typeof(string));
dt.Rows.Add("Test");
dt.Rows.Add("Test2");
dt.Rows.Add("Test3");
dt.Rows.Add("test1");
dt.Rows.Add("test2");
dt.CaseSensitive = true; // this will allow case sensitive sorting
dt.DefaultView.Sort = "Subject asc";
grd.DataSource = dt;
grd.DataBind();
}
For more details refer this link How to sort a DataView in a case-insensitive manner?
Please mark this answer useful if this solve your problem.

I think you have to use the Grid's Sorting event to do a custom sort:
http://www.nullskull.com/a/866/aspnet--sorting-a-gridview-bound-to-a-custom-data-object.aspx
Otherwise if you don't want to do that, just have the database return another column with all lowercase text. Then in the gridView Column, where you have the current Test, Test2,etc...display both in there, have one be a hidden label and set the sort to that column. I haven't tried this but it might work, so in that's columns ItemTemplate have both output but only show one, then sort on the hidden one.

Related

datagridview in asp.net retrieve value

i am working on visual stdio 2008 and my database is in sql server 2005
MY table has three columns
1. SenderName
2. RecieverName
3. Message
i have displayed this table in GridView and add a button named as Reply
so my grid view look's some what like this
SenderName|RecieverName| MessAge|REPLY BUTTON
now this what i want to do
when Button is Clicked in My gridView i need to get data of that specific row
i.e Sender's NAme so that i can Reply him/her ?
can any one help????
Here's a sample:
Markup:
<asp:GridView
runat="server"
ID="gvEmails"
OnSelectedIndexChanged="gvEmails_SelectedIndexChanged">
<Columns>
<asp:ButtonField CommandName="Select" ButtonType="Button" Text="Send" />
</Columns>
</asp:GridView>
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("SenderName");
dt.Columns.Add("ReceiverName");
dt.Columns.Add("Message");
DataRow dr;
dr = dt.NewRow();
dr["SenderName"] = "John Doe";
dr["ReceiverName"] = "Jane Doe";
dr["Message"] = "Hi, Jane.";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["SenderName"] = "Michelle Smith";
dr["ReceiverName"] = "Mike Smith";
dr["Message"] = "Yo, Mike.";
dt.Rows.Add(dr);
gvEmails.DataSource = dt;
gvEmails.DataBind();
}
protected void gvEmails_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gvEmails.SelectedRow;
Response.Write("Send email to " + row.Cells[1].Text);
}
there is a selected index changed function in the properties.
Captuer the selected index and get the cell value of that selected index.
Then continue whtever u want.
There are many different ways of doing this. The easiest, if you only need a single value, would be to bind the value to the CommandArgument of your Reply button. Then add an OnClick handler to your button. Then in the OnClick method you can get the name from the CommandArgument.
If you need more than a single value from the row, you will need to do a little more work. You can setup an event handler on the GridView to capture the event of the index changing. This will provide some event arguments that has a NewSelectedIndex. That will tell you what row was selected. Depending on how your data is bound to your GridView, you can access the data again to get the values you need, or you can set the columns to be a DataKey in the GridView and access them that way.

How to hide columns in an ASP.NET GridView with auto-generated columns?

GridView1.Columns.Count is always zero even SqlDataSource1.DataBind();
But Grid is ok
I can do
for (int i = 0; i < GridView1.HeaderRow.Cells.Count;i++)
I rename request headers here
but
GridView1.Columns[i].Visible = false;
I can't use it because of GridView1.Columns.Count is 0.
So how can I hide them ?
Try putting the e.Row.Cells[0].Visible = false; inside the RowCreated event of your grid.
protected void bla_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false; // hides the first column
}
This way it auto-hides the whole column.
You don't have access to the generated columns through grid.Columns[i] in your gridview's DataBound event.
The Columns collection is only populated when AutoGenerateColumns=false, and you manually generate the columns yourself.
A nice work-around for this is to dynamically populate the Columns collection yourself, before setting the DataSource property and calling DataBind().
I have a function that manually adds the columns based on the contents of the DataTable that I want to display. Once I have done that (and then set the DataSource and called DataBind(), I can use the Columns collection and the Count value is correct, and I can turn the column visibility on and off as I initially wanted to.
static void AddColumnsToGridView(GridView gv, DataTable table)
{
foreach (DataColumn column in table.Columns)
{
BoundField field = new BoundField();
field.DataField = column.ColumnName;
field.HeaderText = column.ColumnName;
gv.Columns.Add(field);
}
}
Note: This solution only works if your GridView columns are known ahead of time.
It sounds like you're using a GridView with AutoGenerateColumns=true, which is the default. I recommend setting AutoGenerateColumns=false and adding the columns manually:
<asp:GridView runat="server" ID="MyGridView"
AutoGenerateColumns="false" DataSourceID="MySqlDataSource">
<Columns>
<asp:BoundField DataField="Column1" />
<asp:BoundField DataField="Column2" />
<asp:BoundField DataField="Column3" />
</Columns>
</asp:GridView>
And only include a BoundField for each field that you want to be displayed. This will give you the most flexibility in terms of how the data gets displayed.
I was having the same problem - need my GridView control's AutogenerateColumns to be 'true', due to it being bound by a SQL datasource, and thus I needed to hide some columns which must not be displayed in the GridView control.
The way to accomplish this is to add some code to your GridView's '_RowDataBound' event, such as this (let's assume your GridView's ID is = 'MyGridView'):
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[<index_of_cell>].Visible = false;
}
}
That'll do the trick just fine ;-)
You have to perform the GridView1.Columns[i].Visible = false; after the grid has been databound.
Try this to hide columns in an ASP.NET GridView with auto-generated columns, both RowDataBound/RowCreated work too.
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Or _
e.Row.RowType = DataControlRowType.Header Then // apply to datarow and header
e.Row.Cells(e.Row.Cells.Count - 1).Visible = False // last column
e.Row.Cells(0).Visible = False // first column
End If
End Sub
Protected Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Or _
e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(e.Row.Cells.Count - 1).Visible = False
e.Row.Cells(0).Visible = False
End If
End Sub
In the rowdatabound method for 2nd column
GridView gv = (sender as GridView);
gv.HeaderRow.Cells[2].Visible = false;
e.Row.Cells[2].Visible = false;
#nCdy:
index_of_cell should be replaced by an integer, corresponding to the index number of the cell that you wish to hide in the .Cells collection.
For example, suppose that your GridView presents the following columns:
CONTACT NAME | CONTACT NUMBER | CUSTOMERID | ADDRESS LINE 1 | POST CODE
And you want the CUSTOMERID column not to be displayed.
Since collections indexes are 0-based, your CUSTOMERID column's index is..........? That's right, 2!! Very good.
Now... guess what you should put in there, to replace 'index_of_cell'??
As said by others, RowDataBound or RowCreated event should work but if you want to avoid events declaration and put the whole code just below DataBind function call, you can do the following:
GridView1.DataBind()
If GridView1.Rows.Count > 0 Then
GridView1.HeaderRow.Cells(0).Visible = False
For i As Integer = 0 To GridView1.Rows.Count - 1
GridView1.Rows(i).Cells(0).Visible = False
Next
End If
I found Steve Hibbert's response to be very helpful. The problem the OP seemed to be describing is that of an AutoGeneratedColumns on a GridView.
In this instance you can set which columns will be "visible" and which will be hidden when you bind a data table in the code behind.
For example:
A Gridview is on the page as follows.
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" >
</asp:GridView>
And then in the code behind a PopulateGridView routine is called during the page load event.
protected void PopulateGridView()
{
DataTable dt = GetDataSource();
gv.DataSource = dt;
foreach (DataColumn col in dt.Columns)
{
BoundField field = new BoundField();
field.DataField = col.ColumnName;
field.HeaderText = col.ColumnName;
if (col.ColumnName.EndsWith("ID"))
{
field.Visible = false;
}
gv.Columns.Add(field);
}
gv.DataBind();
}
In the above the GridView AutoGenerateColumns is set to False and the codebehind is used to create the bound fields. One is obtaining the datasource as a datatable through one's own process which here I labeled GetDataSource(). Then one loops through the columns collection of the datatable. If the column name meets a given criteria, you can set the bound field visible property accordingly. Then you bind the data to the gridview. This is very similar to AutoGenerateColumns="True" but you get to have criteria for the columns. This approach is most useful when the criteria for hiding and un-hiding is based upon the column name.
Similar to accepted answer but allows use of ColumnNames and binds to RowDataBound().
Dictionary<string, int> _headerIndiciesForAbcGridView = null;
protected void abcGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (_headerIndiciesForAbcGridView == null) // builds once per http request
{
int index = 0;
_headerIndiciesForAbcGridView = ((Table)((GridView)sender).Controls[0]).Rows[0].Cells
.Cast<TableCell>()
.ToDictionary(c => c.Text, c => index++);
}
e.Row.Cells[_headerIndiciesForAbcGridView["theColumnName"]].Visible = false;
}
Not sure if it works with RowCreated().
Iterate through the GridView rows and make the cells of your target columns invisible. In this example I want to keeps columns 4-6 visible as is, so we skip those:
foreach (GridViewRow row in yourGridView.Rows)
{
for (int i = 0; i < rows.Cells.Count; i++)
{
switch (i)
{
case 4:
case 5:
case 6:
continue;
}
row.Cells[i].Visible = false;
};
};
Then you will need to remove the column headers separately (keep in mind that removing header cells changes the length of the GridView after each removal):
grdReportRole.HeaderRow.Cells.RemoveAt(0);

How do I sort my datagrid?

Editing some legacy code that populates a datagrid entirely in code. I need to order it by two columns but I don't know how. Which event do I hook into and what can I do to order the 2nd and 3rd columns (that contain dates) in order of most recent first?
Edit: Argh it's a datagrid
Datagrid or GridView?
you can create a dataview and sort it (in the constuctor) then bind this back to the grid in the "sorting" event
something like this might be what you're after
protected void myGridView_Sorting(object sender, GridViewSortEventArgs e)
{
// Your data
DataTable dt = new DataTable();
// Create the view
DataView dv = new DataView(dt, "", "COLUMN_TO_SORT", DataViewRowState.CurrentRows);
// Rebind
myGridView.DataSource = dt;
myGridView.DataBind();
}

Modifying data in new columns in a GridView (ASP.net 3.5 newbie)

my problem: how do I populate extra cells in my gridview during the runtime construction of the grid? (these cells are not bound to any datasource and all I want to do is show some custom text)
I have a gridview on my page and I get 2 columns as bound columns from a fixed database - let's call them
id and URL
I've added a couple of extra columns which are not bound to any datafield but I want to populate the cells with some text which is relevant to the id
for e.g. if the row 1 is
12 http://www.something.com
then I want the extra cell to have 'you have visited this 13 times'
12 http://www.something.com 'you have visited this 13 times'
this text comes from some internal processing I do.
What I want to do is to insert this text when the gridview is being constructed at run time..am totally clueless how to do this. I'm sorry if I'm asking a very dumb question..
You can use the OnRowDataBound event of the GridView to get the current DataItem's content and then set the value of your extra column. You could also just get the cell's content. I think using the DataItem is a bit better, but you can do either.
Something like below
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string myValue = e.Row.Cells[0].Text;
e.Row.Cells[3].Text = " you have visited this " + myValue + " times";
-- or --
MyObjectType myData = e.Row.DataItem as MyObjectType ;
Literal litText = e.Row.FindControl("litText") as Literal;
litText.Text = "you have visited this " + myData.PropertyName + " times";
}
}
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx
http://www.nikhedonia.com/notebook/entry/how-to-show-some-rows-differently-in-a-gridview/
Any way to manipulate the columns in GridView with AutoGenerateColumns = true?

Sorted gridview selects wrong row

I have a gridview (actually a SPgridview)
And i made the columnname clickable so the users can sort the rows using the data.
And that works fine.
The problem occurs when users try to select a row after they sorted the data.
I can see that the gridview kinda "forgets" how the rows were sorted and selects the row that was at the clicked index before it got sorted..
How do i fix that?
I tried sorting the row again after the user selects a row, but that doesnt seem to work.
And should the gridview remember the fact that it was just sorted?
Thanks in advance :)
When you handle the Sorting event set a session variable set it to the sort direction and use it when you rebind your datasource.
protected void gridview_Sorting()
{
// BIND DATA Function
BindData();
DataTable dt = gridview.DataSource as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
Session["sort"] = dt.DefaultView.Sort;
gridview.DataSource = dt;
gridview.DataBind();
}
}
// bind data function//
private void BindData()
{
DataTable dt = GetDataTable();
if (Session["sort"] != null)
{
//Sort the data.
dt.DefaultView.Sort = Session["sort"].ToString();
}
gridview.DataSource = dt;
gridview.DataBind();
}
Make sure that you are not rebinding the grid after a postback.
if(!IsPostBack)
{
gridView.DataSource = yourDataSource;
gridView.DataBind();
}
Are you grabbing the selected row by it's row index or by the unique identifier of the data you are wanting to edit? If you're getting by row index, it may be 'forgetting' since you are recreating the Grid on OnPostBack. Try iterating through the data and select it by it's unique ID, not its row index.
Check Johans blog regarding SPGridView and LinqDataSource
I'd made a number of sortable GridViews, but none with row command interaction before today, when I stumbled on this problem. Mine is a "plain" GridView, not SPgridview. I found this works:
In bindData(), if we have not created a DataTable and put it in the Session object, do so. Otherwise, we'll use the existing, sorted DataTable:
if (Session["dtbl"] == null) {
Session["dtbl"] = method_to_fetch_datatable();
}
gv.DataSource = Session["dtbl"] as DataTable;
gv.DataBind();
In the GridView's handling of row commands that INSERT, UPDATE or DELETE underlying data, refresh the Session object, maintaining the sort, if there is one:
Session["dtbl"] = method_to_fetch_datatable();
if (ViewState["SortExpression"] != null) {
DataTable dt = Session["dtbl"] as DataTable;
dt.DefaultView.Sort = ViewState["SortExpression"] as string;
}
bindData();
I got it working. (kinda)
In the sorting event i saved the sortexpression (the name of the column used to sort by)
and the sortdirection ascending or descending.
Then i make the datasource for the gridview and databind, and after databinding it, i use the gridview.sort command to sort by the values i saved in viewstate.
That works fine, only one problem.
When sorting i made it switch direction after pressing the same column more than one time.
Now it thinks i keep pressing the column title, so it keeps reversing the sorting.
But i tempererarely made it only sort in one direction.
And now im playing with the sender object in the sorting event, im thinking that if i could get some info about whats causing the event i could tell it to only switch direction based on the sender.
Thanks :)

Resources