Prepopulate gridview empty datasource - asp.net

I use an gridview empty datasource for bulk insert, how would I prepopulate for instance Column A with the value of a drop down box? So far I have below, but its not working
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox tb = (TextBox)e.Row.FindControl("YourTextBoxID");
if(tb != null)
{
tb.Text = DropDownList1.SelectedItem.Text;
}
}
}

I don't understand what you mean with empty DataSource. I assume that you actully mean a DataSource which is populated automatically with a default value.
You could use a DataTable:
var tbl = new DataTable();
tbl.Columns.Add("ColumnA");
var defText = DropDownList1.SelectedItem.Text;
for(int i = 0; i < 1000; i++)
{
tbl.Rows.Add(defText);
}
GridView1.DataSource = tbl;
GridView1.DataBind();

Related

How to make dynamic column header clickable in gridview

I have a gridview with many columns. It's sortable, Allow Sorting="True", each column has Sort Expression. For each column sorting works just fine, except for 10 columns that have dynamic headers that I assign in Row_Databound event:
protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < 11; i++)
{
if (Session["Label" + i.ToString()] !=null)
{
e.Row.Cells[i].Text = Session["Label" + i.ToString()].ToString();
}
}
}
}
These 10 columns are not clickable. Is there any way to make them clickable? Everything else in these columns is enabled for sorting.
I've got some suggestions from a different forum about creating columns in Page_Load or Page_Init events, but this probably won't work for me.
Thank you.
You can replace the text of the existing LinkButton in the header cell:
protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < 11; i++)
{
string caption = Session["Label" + i.ToString()] as string;
if (caption != null)
{
TableCell headerCell = e.Row.Cells[i];
LinkButton lnkSort = headerCell.Controls[0] as LinkButton;
lnkSort.Text = caption;
}
}
}
}
It can be done. If you look at the HTML code you will see something similar to this as the link for sorting the GridView.
yourColumnName
We need to recreate that link in the RowDataBound function.
for (int i = 1; i < 11; i++)
{
//first we cast the sender as a gridview
GridView gv = sender as GridView;
//get the unique ID of the gridview, this is different from ClientID which you normally would use for JavaScipt etc
string uniqueID = gv.UniqueID;
//then get the SortExpression for the column
string sortExpression = gv.Columns[i].SortExpression;
//get the new column name from the session
string yourColumnName = string.Empty;
if (Session["Label" + i.ToString()] != null)
{
yourColumnName = Session["Label" + i.ToString()].ToString();
}
//and then we fill the header with the new link
e.Row.Cells[i].Text = "" + yourColumnName + "";
}
However for this to work, enableEventValidation has to be set to false, which is NOT recommended. Otherwise you will get the "Invalid postback or callback argument" error.
Better would be changing the column names somehow before the data is bound to the gridview.
Thank you very much for your help. The solution with LinkButton worked great for me:
protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < 11; i++)
{
if (Session["Label" + i.ToString()] !=null)
{
((LinkButton)(e.Row.Cells[i].Controls[0])).Text = Session["Label" + i.ToString()].ToString();
}
}
}
}

Dynamic template field in grdivew post back values

//Bedlow is the page_load code to add dynamic controls in page load. and load of page_load
gvSecond 's dynamic template fields will be added.I want to know why the dot is appended on each postback values
protected void Page_Load(object sender, EventArgs e)
{
gvFirst.DataSource = GetData("select top 10 * from Project_Master");
gvFirst.DataBind();
}
//gvFirst has gvSecond and on rowdatabound of the gvFirst gvSecond populated with dynamic template fields. and all the template fields has the TextBoxtes in it
protected void gvFirst_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//below code to generate dynamic template column
gvFirst.DataSource = GetData("select top 10 * from Project_Master");
gvFirst.DataBind();
//Added dynamic controls in gvSecond
protected void gvFirst_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string customerId = gvFirst.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvSecond = e.Row.FindControl("gvSecond") as GridView;
DataTable dt= GetData1(Convert.ToInt32(customerId));
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
TemplateField bfield = new TemplateField();
//Initalize the DataField value.
bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
//Initialize the HeaderText field value.
bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);
//Add the newly created bound field to the GridView.
gvSecond.Columns.Add(bfield);
}
TotalColumns = dt.Columns.Count;
gvSecond.DataSource = dt;
gvSecond.DataBind();
}
}
}
//This is used to add the dynamic template in gvSecond.Now when I click on button textbox values appended with postback values and older values seperated by dot(.)
You need to wrap the code in Page_Load in if(!IsPostback){}:
EDIT :
Edited to create second grid at postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
gvFirst.DataSource = GetData("select top 10 * from Project_Master");
gvFirst.DataBind();
}
else
{
CreateSecondGridView();
}
}
And your Rowdatabound may look like this:
protected void gvFirst_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
CreateSecondGridView();
}
protected void CreateSecondGridView()
{
foreach(GridViewRow row in gvFirst.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string customerId = gvFirst.DataKeys[row.RowIndex].Value.ToString();
GridView gvSecond = row.FindControl("gvSecond") as GridView;
DataTable dt= GetData1(Convert.ToInt32(customerId));
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
TemplateField bfield = new TemplateField();
//Initalize the DataField value.
bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
//Initialize the HeaderText field value.
bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);
//Add the newly created bound field to the GridView.
gvSecond.Columns.Add(bfield);
}
TotalColumns = dt.Columns.Count;
gvSecond.DataSource = dt;
gvSecond.DataBind();
}
}
}

Change row color of gridview by database Values

I am making a application in asp.net which shows the values in gridview from the database.In database i am having a colmn named as StatusId which has a value of 1 or 2 or 3.
I tried to show the grid view rows in different color by their statusId values. But it never works. How can i do it in asp.net.
Here is my code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Connection.Open();
SqlCommand Command1 = Connection.CreateCommand();
Command1.CommandText = "Select statusColor from status where statusId=(Select statusId from fileInfo where userId=(Select userId from userInfo where email='" + Session["email"].ToString() + "'))";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
using (SqlDataReader reader = Command1.ExecuteReader())
{
while (reader.Read())
{
statusId = reader["statusColor"].ToString();
}
GridView1.RowStyle.BackColor = Color.FromName(statusId);
}
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.Green;
}
SqlCommand com = new SqlCommand("gridcolor", Connection);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#statusId", statusId);
com.Parameters.Add("#statusColor", SqlDbType.NVarChar, 30);
com.Parameters["#statusColor"].Direction = ParameterDirection.Output;
com.ExecuteNonQuery();
string msg = (string)com.Parameters["#statusColor"].Value;
Connection.Close();
}
What is the mistake i am doing here?
EDIT
I have the color codes which are stored in the database named as statusColor. I have to apply those color to these status.
You have statusId having values 1,2,3 and you are passing to Color.FromName which and 1,2,3 are not names of color you can use switch to assign different colors based on statusId .
Color rowColor = Color.Red;
switch(statusId)
{
case 1:
rowColor = Color.Green;
break;
case 2:
rowColor = Color.White;
break;
case 3:
rowColor = Color.Blue;
break;
}
GridView1.RowStyle.BackColor = rowColor ;
You are doing it the wrong way. You have to databind your gridview on either page load or a custom event (say click of a button) & not rowDataBound event. This event occurs when your row is bound with data & you want to change some attributes for each row (as in your case).
You can use Gridview DataBound event to assign colors in following way
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Fetch status Id for current row
int statusId= Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,"UnitsInStock"));
switch (statusId)
{
case (1):
e.Row.BackColor= System.Drawing.Color.Green;
break;
// other cases follow the same
}
}
}
Put the databinding code in either pageLoad or button click event.
protected void grdMyQ_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < grdMyQ.Rows.Count; i++)
{
if (grdMyQ.Rows[i].Cells[13].Text.ToUpper() == "DISCHARGED_PROCESS")
{
grdMyQ.Rows[i].BackColor = Color.Red;
}
}
}

asp.net - GridView dynamic footer row creation problem

I am able to create BoundFields and Footer-rows dynamically like this in my GridView:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CreateGridView();
}
}
private void CreateGridView()
{
GridView1.Columns.Clear();
DataTable dataTable = Book.GetBooksDataSet().Tables[0];
CommandField cf = new CommandField();
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
int colCount = 1;
foreach (DataColumn c in dataTable.Columns)
{
BoundField boundField = new BoundField();
boundField.DataField = c.ColumnName;
boundField.HeaderText = c.ColumnName;
//boundField.FooterText = "---";
if (colCount == 3 || colCount == 5)
{
boundField.ReadOnly = true;
}
GridView1.Columns.Add(boundField);
colCount++;
}
GridView1.ShowFooter = true;
GridView1.DataSource = dataTable;
GridView1.DataBind();
GridViewRow footerRow = GridView1.FooterRow;
Button b = new Button();
b.Text = "Add New";
int i = 0;
footerRow.Cells[i].Controls.Add(b);
foreach (DataColumn c in dataTable.Columns)
{
++i;
TextBox tb = new TextBox();
footerRow.Cells[i].Controls.Add(tb);
}
}
....................................
....................................
....................................
}
But the problem is, when I click the "Add New" - button, it disappears instantly. And, also I am unable to add any event handler to it. Or intercept its actions like this:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "Edit")
{
GridView1.EditIndex = index;
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
//We can get cell data like this
string id = selectedRow.Cells[1].Text;
string isbn = selectedRow.Cells[2].Text;
//This is necessary to GridView to be showed up.
CreateGridView();
}
else if (e.CommandName == "Update")
{
LinkButton updateButton = (LinkButton)e.CommandSource;
DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;
GridViewRow gvr = (GridViewRow)dcfc.Parent;
//The update...................
//Update grid-data to database
UpdateDataInTheDatabase(gvr.Cells[1].Controls);
//Grid goes back to normal
GridView1.EditIndex = -1;
//This is necessary to GridView to be showed up.
CreateGridView();
}
}
One more thing, I have seen some solutions that suggests to handle the GridView's rowBound event. But I need to do it from within Page_load event handler, or in, GridView1_RowCommand event handler.
Dynamically created controls mus be re-created on every postback. Your "Add New" button causes a postback so the dynamically created footer disappears. Is there a reason this grid has to be created dynamically? From the code you posted it appears that you could do this in markup instead. If not, you'll have to re-create the dynamic controls on every postback.
Edited to add:
I played with this a little bit and what's below works in that the grid doesn't disappear and events are handled, but it doesn't actually do anything. Hope this helps.
Markup:
<p><asp:Literal ID="Literal1" runat="server" /></p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnRowCommand="GridView1_RowCommand"
OnRowEditing="GridView1_RowEditing"/>
Code:
protected void Page_Load(object sender, EventArgs e)
{
BindGridView();
}
private DataTable GetBooksDataTable()
{
var dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("Author", typeof(string));
for (int index = 0; index < 10; index++)
{
dt.Rows.Add(index, "Title" + index, "Author" + index);
}
return dt;
}
private void BindGridView()
{
var dt = GetBooksDataTable();
GridView1.Columns.Clear();
GridView1.ShowFooter = true;
var cf = new CommandField();
cf.HeaderText = "Action";
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
for (int index = 0; index < dt.Columns.Count; index++)
{
var boundField = new BoundField();
boundField.DataField = dt.Columns[index].ColumnName;
boundField.HeaderText = dt.Columns[index].ColumnName;
GridView1.Columns.Add(boundField);
}
GridView1.DataSource = dt;
GridView1.DataBind();
var footer = GridView1.FooterRow;
var b = new LinkButton();
b.Text = "Add New";
b.CommandName = "Add New";
footer.Cells[0].Controls.Add(b);
for (int index = 1; index < dt.Columns.Count + 1; index++)
{
var tb = new TextBox();
footer.Cells[index].Controls.Add(tb);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Literal1.Text = e.CommandName;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Literal1.Text = "Editing row index " + e.NewEditIndex.ToString();
}
Move your code from Page_Load to Page_Init. Things added in the Page_Load last only for the lifecycle of one postback.
You'd then be able to add eventhandlers, intercept events etc.

How to add a row in asp.net grid view

So far I have done this, I am not sure whether this is right or wrong
public partial class _Default : System.Web.UI.Page
{
Label l = new Label();
GridView gv = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
GridViewRow gvr = new GridViewRow(i, i, DataControlRowType.DataRow, DataControlRowState.Normal);
gvr.Controls.Add(l);
gv. (what to do here)
}
this.Controls.Add(gv);
}
}
please help
gv.Rows.Add(gvr);
If you're starting with an empty GridView, an easier way to dynamically create x rows is to create a dummy list and then set it to the data source:
var list = new List<string>(10); // replace 10 with number of empty rows you want
// for loop to add X items to the list
gv.DataSource = list;
gv.DataBind();
If you are doing this, I'd recommend doing it with a Repeater. It's a lot easier to manage.
The DataGrid fires the RowCreate event when a new row is created.
Collapse
//OnRowCreated="GridView3_RowCreated"
protected void GridView3_RowCreated(object sender, GridViewRowEventArgs e)
{
//When a child checkbox is unchecked then the header checkbox will also be unchecked.
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState ==
DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkselect");
CheckBox chkBxHeader = (CheckBox)this.GridView3.HeaderRow.FindControl("chkHeader");
chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(
this,'{0}');", chkBxHeader.ClientID);
}
}

Resources