Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Here my problem is i am like to get the datatype of column that i selected in grid . still now i am using
object st = Args.SortExpression.GetType().name;

But here always it return string only..


Thanks in Advance..
seshu
Posted

try now
public void Sort_Grid(object Src, DataGridSortCommandEventArgs Args) 
        { 
            // Here i like to get the selected column data type.. like args.sortexpression.gettype(); 
            Type t = null; ;
            foreach (DataGridViewColumn dc in dgvMappedData.Columns) 
            { 
                if (dc.Name == Args.SortExpression) 
                    t = dc.ValueType; 
            }
            switch (t)
            {
                case typeof(string):
                    // TODO your code goes here
                    break;
                default:
                    break;
            }
        }
 
Share this answer
 
Comments
hiseshu 16-May-11 5:55am    
Hi nit_singh

here i am unable to get dc.Name and dc.ValuType instead of dc.Name i used dc.SortExpression and dc.ValuType i used dc.sortexpression.getvalue();

HELP me singh i am new for this ...

sorry to say it wont works still it returns string type only for any data type...

Seshu
nit_singh 16-May-11 5:57am    
If still it didn't work than check your Columns DataType. they Should be string only.

Can you paste the code of the binding part of the grid
hiseshu 16-May-11 6:04am    
Dataview dv = log.DefaultView;

private void BindGrid()
{
MyDataGrid.DataSource = dv;
MyDataGrid.DataBind();
}

private DataTable log
{
get
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("No", typeof(string)));

dt.Columns.Add(new DataColumn("Type", typeof(string)));
dt.Columns.Add(new DataColumn("Date", typeof(DateTime)));
dt.Columns.Add(new DataColumn("Status", typeof(string)));
dt.Columns.Add(new DataColumn("Remarks", typeof(string)));

DB_RecordsetReadOnly rs = new DB_RecordsetReadOnly(stSQL);
SqlDataReader tbMySQL = rs.Recordset;
hiseshu 16-May-11 6:37am    
Hi

did u checked my code.. if possible once check it and let me know for changes please

Seshu
If you want to get the Type you should do this

C#
Type t = Args.SortExpression.GetType();


The *.GetType().Name returns a string.
 
Share this answer
 
Comments
hiseshu 16-May-11 3:59am    
Hi

Thanks for spending time .. still my problem is same i am getting string as output...
hiseshu 16-May-11 4:00am    
This is the function where i am trying to use it is

public void Sort_Grid(object Src, System.Web.UI.WebControls.DataGridSortCommandEventArgs Args)
{
type t=args.sortExpression.GetType();
}
but it return a sting ...
Wayne Gaylard 16-May-11 4:10am    
What exactly are you trying to do once you have the Type ?
hiseshu 16-May-11 4:25am    
Hi

Here based on the type of the column i like to sort...

Seshu
 
Share this answer
 
Try that way

dgvMappedData.Columns.Cast<DataGridViewColumn>().ToList().Find(dc =>dc.Name == Args.SortExpression).ValueType


It should work

Args.SortExpression contains the name of the column as string so you will get string as the type.
 
Share this answer
 
v2
Comments
hiseshu 16-May-11 4:35am    
hi nit_singh

Thanks for spending. It wont works here can u suggest if there is any alternative for getting the type particular column from a grid ..

Seshu
nit_singh 16-May-11 4:47am    
Why is it not working? What errors you are getting. Explain..
hiseshu 16-May-11 4:56am    
hi

MyDataGrid.Columns.Cast<args.sortexpression>().ToList().Find(dc=>dc.Name==Args.SortExpression).valueType;


i am getting error near by >

Is this code correct...

Seshu
nit_singh 16-May-11 4:59am    
Yes Seshu, Which .net version u r using. This is lambda expression.
hiseshu 16-May-11 5:02am    
i am using 2.0
Try that way

Type t = dgvMappedData.Columns.Cast<datagridviewcolumn>().ToList().Find(
                            delegate(DataGridViewColumn dc)
                            {
                                return dc.Name == "Name";
                            }
                          ).ValueType;
</datagridviewcolumn>
 
Share this answer
 
Comments
hiseshu 16-May-11 5:11am    
The type or namespace name 'args.sortexpression' could not be found (are you missing a using directive or an assembly


getting this error.. can u once check the last comment once about how function i am using..

Thanks
seshu

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900