Hi need some help to Change column header and hide a column in Gridview. This: (grdVendors.Columns[1].HeaderText= "test"; ) doesn't work. Please see the image ( http://www.vengalia.com/HIDEANDRENAMEHEADER.jpg ) to see what DataSource contains.
CODE:
protected void drpSearchBy_SelectedIndexChanged(object sender, EventArgs e)
{
int placesID = Convert.ToInt32(drpSearchBy.SelectedValue.Trim());
VendorCollection places = VendorCollection.GetbyPlaceID(placesID);grdVendors.DataSource = places;
//grdVendors.Columns[1].HeaderText= "test";
grdVendors.DataBind();
}
Thanks for any help.
Raul
Loading
Raul JuarezPosted Nov 18, 2009, 11:14 PM
<tr>
<td colspan=5>
<asp:GridView ID="gridVendor" runat="server" AutoGenerateColumns="False" EnableViewState="false" CellPadding=5>
<Columns>
<asp:TemplateField HeaderText="Vendor Name">
<ItemTemplate>
<%# ((Vendor)Container.DataItem).VendorName %>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<%# ((Vendor)Container.DataItem).VendorAddress %>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<%# ((Vendor)Container.DataItem).VendorCity %>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<%# ((Vendor)Container.DataItem).VendorState %>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="ZipCode">
<ItemTemplate>
<%# ((Vendor)Container.DataItem).VendorZipCode %>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<a href="~/OrderOnlineShoeRepair.aspx?VendorID=<%# ((Vendor)Container.DataItem).VendorID.ToString() %>">Edita>
ItemTemplate>
asp:TemplateField>
Columns>
asp:GridView>
Nilanka DharmadasaPosted Nov 18, 2009, 11:11 PM
But I have no idea about your base classes.
Just check whether these requirements are satisfied in your code.
'VendorCollection' should be an array or some sort of list which is implemented by IList interface. What is the type of your class?
Then 'VendorCollection' should be a collection of 'Vendor' objects.
Check these and let me know.
Raul JuarezPosted Nov 18, 2009, 9:37 PM
1.-VendorCollection.cs
using System;
using ShoeRepair.DataAccessLayer.SQL;
namespace ShoeRepairNY.Business
{
public class VendorCollection : ShoeRepairBaseCollection<Vendor>
{
public static VendorCollection GetbyPlaceID(int placeID)
{
VendorCollection obj = new VendorCollection();
obj.MapObjects(new VendorDataService().Vendor_GetByPlaceID(placeID));
return obj;
}
}
2.- VENDOR.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShoeRepairNY.Common;
using System.Data;
using ShoeRepair.DataAccessLayer.SQL;
namespace ShoeRepairNY.Business
{
public class Vendor : ShoeRepairBaseObject
{
#region Fields
private int _vendorID = Constants.NullInt;
private string _vendorName = Constants.NullString;
private string _vendorAddress = Constants.NullString;
private string _vendorAddress2 = Constants.NullString;
private string _vendorCity = Constants.NullString;
private string _vendorState = Constants.NullString;
private string _vendorZipCode = Constants.NullString;
private string _vendorPhone = Constants.NullString;
private string _vendorFax = Constants.NullString;
private string _vendorEmail = Constants.NullString;
#endregion
#region Properties
public int VendorID
{
get { return _vendorID; }
set { _vendorID = value; }
}
public string VendorName
{
get { return _vendorName; }
set { _vendorName = value; }
}
public string VendorAddress
{
get { return _vendorAddress; }
set { _vendorAddress = value; }
}
public string VendorAddress2
{
get { return _vendorAddress2; }
set { _vendorAddress2 = value; }
}
public string VendorCity
{
get { return _vendorCity; }
set { _vendorCity = value; }
}
public string VendorState
{
get { return _vendorState; }
set { _vendorState = value; }
}
public string VendorZipCode
{
get { return _vendorZipCode; }
set { _vendorZipCode = value; }
}
public string VendorPhone
{
get { return _vendorPhone; }
set { _vendorPhone = value; }
}
public string VendorFax
{
get { return _vendorFax; }
set { _vendorFax = value; }
}
public string VendorEmail
{
get { return _vendorEmail; }
set { _vendorEmail = value; }
}
#endregion
public override bool MapData(DataRow row)
{
VendorID = GetInt(row, "VendorID");
VendorName = GetString(row, "VendorName");
VendorAddress = GetString(row, "VendorAddress");
VendorAddress2 = GetString(row, "VendorAddress2");
VendorCity = GetString(row, "VendorCity");
VendorState = GetString(row, "VendorState");
VendorZipCode = GetString(row, "VendorZipCode");
VendorPhone= GetString(row, "VendorPhone");
VendorFax = GetString(row, "VendorFax");
VendorPhone = GetString(row, "VendorPhone");
return base.MapData(row);
}
}
}
3.Default
protected void drpSearchBy_SelectedIndexChanged(object sender, EventArgs e)
{
int placesID = Convert.ToInt32(drpSearchBy.SelectedValue.Trim());
VendorCollection places = VendorCollection.GetbyPlaceID(placesID);
grdVendors.DataSource = places;
grdVendors.DataBind();
}
Lalit MPosted Nov 18, 2009, 5:28 AM
Nilanka DharmadasaPosted Nov 18, 2009, 5:12 AM
I have a simple example for you.
Class1
-------
public class Class1
{
private string prop1;
private string prop2;
private string prop3;
public string property1
{
get { return prop1; }
set { prop1 = value; }
}
public string property2
{
get { return prop2; }
set { prop2 = value; }
}
public string property3
{
get { return prop3; }
set { prop3 = value; }
}
}
Main Class
----------
private void button1_Click(object sender, EventArgs e)
{
Class1[] v = new Class1[2];
Class1 a = new Class1();
a.property1 = "aa";
a.property2 = "bb";
a.property3 = "cc";
v[0] = a;
Class1 b = new Class1();
b.property1 = "dd";
b.property2 = "ee";
b.property3 = "ff";
v[1] = b;
dataGridView1.DataSource = v;
}
If you run this, you will see it adds 3 columns to the datagridview with the names of the properties. And it will add two rows with the given data.
But in your code, have you defined properties in your class? Eventhough the datasource it set with the object, it doesn't add columns to your datagridview. You must define prperties.
Tell me if you still have problems.
Raul JuarezPosted Nov 18, 2009, 1:52 AM
Am not sure to use xmlserializer to convert "VendorCollection" to xml then load XML into DATASET and finally set grdVendors.DataSource = DATASET;
Pleae if you have any other ideas I will appreciate, thanks for your help.
SEE RELATED CODE BELOW
FILE 1: VendorCollection.cs
namespace ShoeRepairNY.Business
{
public class VendorCollection : ShoeRepairBaseCollection<Vendor>
{
public static VendorCollection GetbyPlaceID(int placeID)
{
VendorCollection obj = new VendorCollection();
obj.MapObjects(new VendorDataService().Vendor_GetByPlaceID(placeID));
return obj;
}
}
}
FILE2: DEFAULT.CS
protected void drpSearchBy_SelectedIndexChanged(object sender, EventArgs e)
{
int placesID = Convert.ToInt32(drpSearchBy.SelectedValue.Trim());
VendorCollection places = VendorCollection.GetbyPlaceID(placesID);
grdVendors.DataSource = places;
grdVendors.DataBind();
}
Nilanka DharmadasaPosted Nov 18, 2009, 1:24 AM
According to the error, I'm sure that the problem is not in seeting the header part.
Error is in setting the datasource.
Seems your datagridview has no columns eventhough you try to access them.
Didn you use a binding source to bind the array?
Raul JuarezPosted Nov 18, 2009, 12:47 AM
http://www.vengalia.com/HIDEHEADERCHANGE.jpg
Nilanka DharmadasaPosted Nov 17, 2009, 11:11 PM
To hide a column,
grdVendors.Columns[1].Visible = false;
To change header text ,
grdVendors.Columns[1].HeaderText = "test";
From the code you have given, I cant tell you the exact reason. If you can, please paste the full code, so that I can run it and see.
If you find this answer helpful, please mark this as accepted.