dear sir/madam,
This is vishnuvardhan,I have some doubt on working with datagrid view in windows forms.Here my doubt is i bind a table to datagridview which is in windows form by using OLEDB connection.That table contains coluimns as A,B,C,D,E,F,G,H,I.Each column contains some data multiple().I placed one combo box .i want to sort data in "E" column.that column contain data(as J,K,L,M).Total four items,but total columns are 20.So each J,K,L,M are repeate.
here my intenssion is sort perticular item(J,K,L,M) in runtime by using combobox.Could you please refer description document which was in attachments.Please help me to develop like dis.
Thanks in advance.
Loading
Amit ChoudharyPosted Mar 4, 2011, 12:50 AM
Add below code to Form1.Designer.cs InitializeComponent() method:
this.dataGridView1.ColumnHeaderMouseClick+=new System.Windows.Forms.DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
Now in Code behind of Form1.cs
declare a variable int cntr=0; at class level like,
public partial class Form1 : Form
{
int cntr = 0;
....
....
//Add this event handler
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (cntr % 2 == 0)
dataGridView1.Sort(dataGridView1.Columns[e.ColumnIndex], ListSortDirection.Ascending);
else
dataGridView1.Sort(dataGridView1.Columns[e.ColumnIndex], ListSortDirection.Descending);
cntr++;
}
}
I have run the code at my end. and its working just great.
Thanks.
Amit ChoudharyPosted Mar 2, 2011, 1:15 AM
You can sort your grid by sorting its datasource and rebind it.. for e.g.,
private void Sort_DataGrid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
// Never use Queries like this always use Stored procedures
SqlCommand myCommand = new SqlCommand("SELECT * FROM Categories", myConnection);
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "Categories");
DataView dv = new DataView(ds.Tables["Categories"]);
if ((numberDiv % 2) == 0)
dv.Sort = e.SortExpression + " " + "ASC";
else
dv.Sort = e.SortExpression + " " + "DESC";
numberDiv++;
myDataGrid.DataSource = dv;
myDataGrid.DataBind();
}
Here i wrote the code in Sort_Grid event now what you can do is you can create a combobox event and you can do the same operation based on you selection from combo.
For more info you can refer to this article by mahesh:
http://www.c-sharpcorner.com/UploadFile/mahesh/DataGridCustomizationPart-II11242005045146AM/DataGridCustomizationPart-II.aspx
Hope this helps you.
Thanks
vishnuvardhan diyyalaPosted Mar 2, 2011, 3:27 PM
i m unable to find that sorting event.now i attach zip file of my project.Could you please check and suggest how to solve my problem.Thanks in advance.
Amit ChoudharyPosted Mar 2, 2011, 3:10 AM
vishnuvardhan diyyalaPosted Mar 2, 2011, 2:50 AM
I m unable to analyse the following lines
if ((numberDiv % 2) == 0)
dv.Sort = e.SortExpression + " " + "ASC";
else
dv.Sort = e.SortExpression + " " + "DESC";
numberDiv++;
myDataGrid.DataSource = dv;
myDataGrid.DataBind();
i wrote code as per u r previous answer.But sortExpression is not find
please clarify how to writ coder for sort event
e.sort expression in not find in intellisence.
i attach the code as datagrid.rar.
Please refer that once
if any errors there please suggest me the right.
Thanks in advance.