Changing the columns titles on a GridView if I set the datasource on codebehind? - asp.net

How do I set the GridView Category Columns Titles manually if I'm databinding manually?
namespace Workforce
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var s = Work.DataLayer.Connection("test");
var x = Work.DataLayer.GetCourseList(s);
GridView1.DataSource = x;
GridView1.DataBind();
}
}
}
In the design view, there is a data source id which i'm not using.

How about this
GridView1.HeaderRow.Cells[0].Text = "New Header";

Like #Rahul stated, you want to do this in the RowDataBound event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Your Better Column Header";
}
}
And repeat for each row header you want to modify, ensuring you have used the correct ordinal number.

Related

GridView Editing when bound to a Datatable (no database)

I am a beginner at ASP.NET and I have been trying to allow editing/updating of a GridView that is bound to a Datatable, without apparent success.
The GridView, named "WeightGridView", has two columns, "Asset Class" and "Weight", and is populated upon the click of a button "AddAssetsButton". Once you click on this button, the GridView will be populated with the values of a listbox, "ListBox2". The code for this event handler is:
protected void AddAssetsButton_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Asset Class", typeof(string));
table.Columns.Add("Weight", typeof(double));
foreach (ListItem li in ListBox2.Items)
{
DataRow dr = table.NewRow();
dr["Asset Class"] = li.Value;
dr["Weight"] = 0;
table.Rows.Add(dr);
}
WeightGridView.DataSource = table;
WeightGridView.DataBind();
SelectAssetsButton_ModalPopupExtender.Hide();
}
I have created a RowEditing and RowUpdating functions for the GridView, but they don't seem to work properly.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
WeightGridView.DataBind();
}
}
protected void WeightGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
WeightGridView.EditIndex = e.NewEditIndex;
}
protected void WeightGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string weight;
weight = ((TextBox)WeightGridView.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
WeightGridView.EditIndex = -1;
WeightGridView.DataBind();
}
Should you need any additional code to answer this post, please let me know and I will update the post.
Any help will be appreciated! Thank you!

Changing the style of specific records in GridView

I'm designing a logistics system in ASP.Net . In the Order processing page, orders are displayed by Grid View,i want to change the font style of the rows to BOLD, which are marked as"ordedr not processed". thanks.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string OrStatus = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Orderstatus"));
if (OrStatus == "Order not processed")
{
//You can use whatever you want to play with rows
e.Row.Cells[0].Font.Bold = true;
e.Row.Cells[2].CssClass = "gridcss";
}
}
}
Follow that code. It will helps
You can do this in "rowdatabound" event of grid.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView grid = GridView1;
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
string orderstatus= Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Orderstatus"));
if(orderstatus=="Order not processed)
{
//write your code to change css
}
}
}

How to remove row from gridview on row command event?

I am trying to remove row from gridview by click on ImageButton which is placed inside gridview. I am getting row index but dont know how to remove. My gridview binds from session and i dont want to rebind gridview.
Here is my code:
protected void GVDetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Remove"))
{
List<Class1> list = (List<Class1>)Session["value1"];
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gridRow = gvDetail.Rows[index];// (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
((List<Class1>)Session["value1"]).RemoveAt(index);
}
}
If anyone have any idea about this than please help me..
Use the gridview deleterow method.
void GVDetail_RowCommand_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Remove")
{
var id = Int32.Parse(e.CommandArgument);
GVDetail.DeleteRow(id);
}
}
protected void GVPurchaseOrderTax_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
if (dt.Rows.Count > 0)
{
dt.Rows[e.RowIndex].Delete();
GVPurchaseOrderTax.DataSource = dt;
GVPurchaseOrderTax.DataBind();
}
}
}

How to use ColumnName in GridView control to hide some columns

I want to hide few columns of a gridview before they gets displayed.
I want to do it by create a common function which can be used by multiple controls.
I am using an extension and would like to know how it can be done.
Here is my code
protected void btnStandardView_Click(object sender, EventArgs e)
{
_viewTypeDl = new ViewTypeDL();
DataTable dt = _viewTypeDl.GetStandardView();
gvViewType.Source(_viewTypeDl.GetStandardView(),"ColorCode");
ViewState["request"] = "Standard View";
}
public static void Source(this CompositeDataBoundControl ctrl, DataTable dt, params string[] ColumnsToHide)
{
ctrl.DataSource = dt;
ctrl.DataBound += new GridViewRowEventHandler(ctrl_DataBound);
ctrl.DataBind();
}
static void ctrl_DataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells["ColorCode"].Visible = false;
}
I want to create an extension to hide or show columns provided in the list as an array.
1st function is used on page. While below two functions are needs to be used for multiple applications
There are two ways you can meet your requirement.
set gvViewType.Columns[i].visble = false;
Allow css to handle the hidden columns for you.
.hidden
{
display:none;
}
.visble
{
display:block;
}
//This is the Gridview event.
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Cells Represent the Column
e.Row.Cells[0].CssClass = "hidden";
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].CssClass = "hidden";
}
}

asp.net making a gridView column invisible

This is a Master-Detail form. Master is a GridView. And, the Detail is a DetailsView.
The entire thing is achieved programmatically.
As you can see from the code, DetailsView is using the Master-objects's ID to retrieve the Detail items.
I need to make the ID column of the Master-GridView invisible. Coz, it is irrelevent for the user of the page. But it must not harm the page logic.
But the code-line, GridView1.Columns[1].Visible = false; is generating an exception.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
How should I solve this problem?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
List<Order> orders = Order.Get();
GridView1.DataSource = orders;
GridView1.DataBind();
// This is giving Error...............!!!
GridView1.Columns[1].Visible = false;
// At first, when the page first loads,
// GridView1.SelectedIndex == -1
// So, this is done to automatically select the 1st item.
if (GridView1.SelectedIndex < 0)
{
GridView1.SelectedIndex = 0;
}
int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView1.Rows[selRowIndex].Cells[1].Text);
Order master = Order.Get(selMasterId);
labItemsCount.Text = master.Items.Count.ToString();
DetailsView1.DataSource = master.Items;
DetailsView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
BindData();
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
BindData();
}
}
Have you considered using the DataKeyNames property of the gridview? This way you can remove the 'id' column from the GridView bu still access the 'id' value in the Page_Load.
DataKeyNames = "id"
Then you can get the value of the id like this.
int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView.DataKeys[selRowIndex].Value);
Order master = Order.Get(selMasterId);
Alternately, you could try changing the visibility of the column in the OnRowBound event of the GridView.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Visible = false;
}
}

Resources