Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have successfully bind data in datagridview from web service. but it binds all the columns. but i want only some columns to be binded..
Posted
Comments
Raajkumar.b 4-Feb-14 4:48am    
May be you are binding table to datagridview which the table consists of so many columns.

Fill the table with particular columns what do you want to bind to datagridview then bind that table to datagridview.

Set autogeneratecolumns="false" in gridview property..: )

And bind gridview using boundfields.. :)

like that

C#
<asp:gridview id="gvMessages" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown">
    CaptionAlign="NotSet" CellPadding="5">
    <columns>
        <asp:boundfield headertext="FielsHeader1" datafield="Fields1" />
        <asp:boundfield headertext="FielsHeader2" datafield="Fields1" />
    </columns>
</asp:gridview>
 
Share this answer
 
Hope this helps you
C#
[WebMethod]
    public static List<record> GetData(string param)    
    {
        SqlConnection con = new SqlConnection(@"ConnectionString");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT type,desc,num,ref,date FROM foo", con);
        SqlDataReader dr = cmd.ExecuteReader();

        List<record> records = new List<record>();

        while (dr.Read())
        {
            records.Add(new Record() {
                AccountType = dr.GetString(0),
                PartDescription = dr.GetString(1),
                PartNumber = dr.GetInt32(2),
                OrderRef = dr.GetString(3),
                TransactionDate = dr.GetDateTime(4)                
            });
        }

        dr.Close();
        con.Close();
        return records;
    }</record></record></record>
 
Share this answer
 
IF you are getting the result in DataTable format
you can remove the unwanted columns like this


C#
string[] columnsToBeRemoved = { "column1", "Column2" };
       foreach (string column in columnsToBeRemoved)
       {
          bool isValidColumn= dt.Columns.OfType<DataColumn>().Select(k => k.ColumnName).Any(k => k == column);
          if (isValidColumn)
              dt.Columns.Remove(column);
       }
 
Share this answer
 

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