private void searchgrid()
{
GridViewRow HeaderGridRow = new GridViewRow(0, 1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "";
TextBox abc = new TextBox();
HeaderCell.Controls.Add(abc);
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "";
TextBox search = new TextBox();
search.ID = "search1";
HeaderCell.Controls.Add(search);
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
Button btn_chk = new Button();
btn_chk.Text = "go";
HeaderCell.Controls.Add(btn_chk);
HeaderCell.ColumnSpan = 1;
btn_chk.Click += new EventHandler(btnClick);
HeaderGridRow.Cells.Add(HeaderCell);
//tbl1.Controls.Add(HeaderGridRow);
//btn_chk.CommandName = "btn_chk";
GridView2.Controls[0].Controls.AddAt(0, HeaderGridRow);
//GridView2.Controls[0].Controls.AddAt(0, tbl1);
}
i have a gridview,inside this there is another dynamically created gridview which hAS a textbox.how to find control of this textbox.by looping i get the values but i don get the desired output..so i need just one textbox value..please help
You can do it without a loop like this:
GridView gv = GridView1.Rows[0].FindControl("GridView2") as GridView;
TextBox tb = gv.Rows[0].FindControl("TextBox1") as TextBox;
string tbValue = tb.Text;
You find the nested GridView first and cast it, then you can find the TextBox in the nested GridView.
And you could still always use a loop:
foreach (GridViewRow parentRow in GridView1.Rows)
{
if (parentRow.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)parentRow.FindControl("GridView2");
foreach (GridViewRow childRow in gv.Rows)
{
if (childRow.RowType == DataControlRowType.DataRow)
{
TextBox tb = (TextBox)childRow.FindControl("TextBox1");
string tbValue = tb.Text;
}
}
}
}
I have a gridView I want to set its first 11 columns as Heading like="NMAT APPLICANT DETAILS". My gridview name is GridView1. How can I set this?
I done it by below mentioned method. Now how can I adjust this heading center of this 11 cells??
My code
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView HeaderGrid = (GridView)sender;
GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "NMAT APPLICANT DETAILS";
HeaderCell.ColumnSpan = 11;
HeaderGridRow.Cells.Add(HeaderCell);
GridView1.Controls[0].Controls.AddAt(0, HeaderGridRow);
}
}
nothing was there I hust done like below mentioned methode
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
Basically I create Rows then Cells then i populate the Cells with LinkButton which is getting Text value from XML nodes .
Code :
private void linkBTN_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
string text = btn.Text.ToString();
XmlDocument clickDoc = new XmlDocument();
clickDoc.Load(Server.MapPath("~/ProductShow.xml"));
XmlNodeList btnNode = clickDoc.SelectNodes("products/" + text.ToString() + "/*");
int count = btnNode.Count;
foreach (XmlNode node in btnNode)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.CssClass = "Cell";
LinkButton linkbtn = new LinkButton();
linkbtn.Text = node.InnerText;
linkbtn.Attributes.Add("runat", "server");
Image img = new Image();
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(img);
row.Cells.Add(cell);
MainTable.Rows.Add(row);
}
}
My problem is that i want to create 4 cells for each row and my code creates 1 cell for each row untill the Nodes end
You need another loop to add more columns to each row
foreach (XmlNode node in btnNode)
{
TableRow row = new TableRow();
for (int i = 0; i < 4; i++){
TableCell cell = new TableCell();
cell.CssClass = "Cell";
LinkButton linkbtn = new LinkButton();
linkbtn.Text = node.InnerText;
linkbtn.Attributes.Add("runat", "server");
Image img = new Image();
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(img);
row.Cells.Add(cell);
}
MainTable.Rows.Add(row);
}
GridView gv = new GridView();
BoundField farmername = new BoundField();
farmername.HeaderText = "Farmer Name";
farmername.DataField = "farmername";
gv.Columns.Add(farmername);
BoundField villagename = new BoundField();
villagename.HeaderText = "Village Name";
villagename.DataField = "village";
gv.Columns.Add(villagename);
BoundField feedtype = new BoundField();
feedtype.HeaderText = "Feed Type";
feedtype.DataField = "feedtype";
gv.Columns.Add(feedtype);
BoundField bf50kg = new BoundField();
bf50kg.HeaderText = "50 Kg Bags";
bf50kg.DataField = "noof50kgsbags";
gv.Columns.Add(bf50kg);
CommandField cf = new CommandField();
cf.ButtonType = ButtonType.Button;
cf.ShowCancelButton = true;
cf.ShowEditButton = true;
gv.Columns.Add(cf);
gv.RowEditing += new GridViewEditEventHandler(gv_RowEditing);
gv.RowUpdating += new GridViewUpdateEventHandler(gv_RowUpdating);
gv.RowCancelingEdit += new GridViewCancelEditEventHandler(gv_RowCancelingEdit);
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
gv.DataSource = dtIndentDetails;
gv.DataBind();
When I clicked on edit button its not spliting into update, Cancel buttons . How can I do this with command field .If I add gridview in aspx page, its splitting to update and cancel
Try the following code:
protected void gridview_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gv = (GridView)sender;
// Change the row state
gv.Rows[e.NewEditIndex].RowState = DataControlRowState.Edit;
}
Tried your code and found it working.
Take care of below points:
1.) The Code creating GridView (and all fields ) should be executed every time. Means remove any !IsPostback condition from this code, If present any.
2.) In your RowEditing event of your gridview set the editindex and rebind the gridview.
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gv = sender as GridView;
gv.EditIndex = e.NewEditIndex;
gv.DataBind();
}
i'm trying to add a new headerrow to a Gridview. This row should appear below the original headerrow.
As far as I know I have two events to choose from:
1.) Gridview_RowDataBound
2.) Gridview_RowCreated
Option 1 is not an option as the grid is not binding the data on each postback.
Option 2 does not work as expected. I can add the row, but it is added before the HeaderRow because the HeaderRow itself is not added yet in this event...
Please assist, thank you!
Code: (InnerTable property is exposed by custom gridview)
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
Dim r As New GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
For Each c As DataControlField In CType(sender, GridView).Columns
Dim nc As New TableCell
nc.Text = c.AccessibleHeaderText
nc.BackColor = Drawing.Color.Cornsilk
r.Cells.Add(nc)
Next
Dim t As Table = GridView1.InnerTable
t.Controls.Add(r)
End If
End Sub
Since this is a custom GridView, why don't you consider overriding the CreateChildControls method?
I.e (sorry, C#):
protected override void CreateChildControls()
{
base.CreateChildControls();
if (HeaderRow != null)
{
GridViewRow header = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
for (int i = 0; i < Columns.Count; i++)
{
TableCell cell = new TableCell();
cell.Text = Columns[i].AccessibleHeaderText;
cell.ForeColor = System.Drawing.Color.Black;
cell.BackColor = System.Drawing.Color.Cornsilk;
header.Cells.Add(cell);
}
Table table = (Table)Controls[0];
table.Rows.AddAt(1, header);
}
}
UPDATE
As was mentioned by Ropstah, the sniplet above does not work with pagination on. I moved the code to a PrepareControlHierarchy and now it works gracefully with pagination, selection, and sorting.
protected override void PrepareControlHierarchy()
{
if (ShowHeader && HeaderRow != null)
{
GridViewRow header = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
for (int i = 0; i < Columns.Count; i++)
{
TableCell cell = new TableCell();
cell.Text = Columns[i].AccessibleHeaderText;
cell.ForeColor = System.Drawing.Color.Black;
cell.BackColor = System.Drawing.Color.Cornsilk;
header.Cells.Add(cell);
}
Table table = (Table)Controls[0];
table.Rows.AddAt(1, header);
}
//it seems that this call works at the beginning just as well
//but I prefer it here, since base does some style manipulation on existing columns
base.PrepareControlHierarchy();
}
Nice work guys, I used your technique for grouping my AJAX enabled gridview, and I searched for a long, long time. Cheers.
protected override void PrepareControlHierarchy()
{
if (GroupColumns)
{
#region Group Column
Table table = (Table)Controls[0];
string lastValue = string.Empty;
foreach (GridViewRow gvr in this.Rows)
{
string currentValue = gvr.Cells[GroupColumnIndex].Text;
if (lastValue.CompareTo(currentValue) != 0)
{
// there's been a change in value in the sorted column
int rowIndex = table.Rows.GetRowIndex(gvr);
// Add a new sort header row
GridViewRow sortRow = new GridViewRow(rowIndex, rowIndex, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell sortCell = new TableCell();
TableCell blankCell = new TableCell();
sortCell.ColumnSpan = this.Columns.Count - 1;
sortCell.Text = string.Format("{0}", currentValue);
blankCell.CssClass = "group_header_row";
sortCell.CssClass = "group_header_row";
// Add sortCell to sortRow, and sortRow to gridTable
sortRow.Cells.Add(blankCell);
sortRow.Cells.Add(sortCell);
table.Controls.AddAt(rowIndex, sortRow);
// Update lastValue
lastValue = currentValue;
}
}
#endregion
}
HideColumns();
base.PrepareControlHierarchy();
}
Try this when you add the row to the InnerTable:
t.Controls.AddAt(1, r)
Here's a quick basic test I did, which seems to work OK:
Protected Sub gridview_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridview.DataBound
Dim g As GridView = CType(sender, GridView)
Dim r As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
Dim th As New TableHeaderCell()
th.ColumnSpan = g.Columns.Count
th.Text = "This is my new header"
r.Cells.Add(th)
Dim t As Table = CType(g.Controls(0), Table)
t.Rows.AddAt(1, r)
End Sub