nested gridview row values - c#

I have this code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="something">
        <Columns>
            <asp:TemplateField >
                <ItemTemplate>
                    <asp:Panel ID="Panel1" runat="server" Style="display: none" >
                        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" ShowHeader="False">
                            <Columns>
<asp:BoundField DataField="NAME"></asp:BoundField>
                              other  asp-BoundFields...
                            </Columns>
                        </asp:GridView>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
             asp-BoundFields...
        </Columns>
</asp:GridView>
CS: In function void Test() I want to get values from each row of gridview2. I tried with findcontrol method, but I think it is not possible to find BoundField. How can I do that?

Found soultion on similar forum.
GridView Gridview2 = (GridView)GridView1.Rows[1].FindControl("GridView2");

Related

templateField Control are lost while deleting the other columns in gridview

I have a gridview :
<asp:GridView ID="grdData" OnRowCreated="GridData_RowCreated" runat="server" DataKeyNames="ID" AutoGenerateColumns="false" OnRowDataBound="grdData_RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="editbtn" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If I am trying to alter the gridview i.e when I am removing other columns except the existing template field the template field becomes empty:
int grdCount = grdData.Columns.Count;
for (int i = 1; i < grdData.Columns.Count; i++)
{
grdData.Columns.RemoveAt(1);
}
how to maintain the templateField Control while deleting the other columns in gridview.?
Please suggest.
Thanks in advance.
//You have to bind your grid view after deleting column.
public void bindGridData()
{
//Add your logic here which binds your grid.
grdData.Databind();
}
//You have to call above method after you delete a column from grid
view.
bindGridData();

nested child gridview name does not exist in this context

So I have created a gridview and inside it I have given another gridview.
<asp:GridView ID="dgInstitute" runat="server" AutoGenerateColumns="False" CellPadding="0"
CellSpacing="0" ShowHeaderWhenEmpty="True" PageSize="100" Width="100%" CssClass="table table-responsive table-bordered"
Visible="true" UseAccessibleHeader="true" OnRowCreated="dgInstitute_RowCreated"
OnDataBound="dgInstitute_DataBound" OnRowDataBound="dgInstitute_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Id" DataField="refGroupId" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="gridHeader panel-default"></asp:BoundField>
<asp:TemplateField HeaderStyle-CssClass="gridHeader panel-default">
<ItemTemplate>
<asp:GridView ID="dgProgram" runat="server" AutoGenerateColumns="False" CellPadding="0"
CellSpacing="0" ShowHeaderWhenEmpty="True" PageSize="100" Width="100%" CssClass="table table-responsive table-bordered"
Visible="true" UseAccessibleHeader="true" OnRowCreated="dgProgram_RowCreated" OnDataBound="dgProgram_DataBound">
<Columns>
<asp:BoundField HeaderText="Id" DataField="refGroupId" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="gridHeader panel-default"></asp:BoundField>
<asp:TemplateField HeaderStyle-CssClass="gridHeader panel-default" ShowHeader="false">
<ItemTemplate>
<asp:Button AutoPostBack="true" Width="300px" CssClass="btn btn-primary btn-embossed" ID="btnProgram" runat="server"
Text='<%#Eval("refValues") %>'
OnClick="btnProgram_Click" CommandArgument='<%#Eval("refGroupId") %>' />
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="panel-default"></HeaderStyle>
<RowStyle CssClass=""></RowStyle>
</asp:GridView>
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="panel-default"></HeaderStyle>
<RowStyle CssClass=""></RowStyle>
</asp:GridView>
For this first gridview is accessible on code behind but when I try to use secondGridview it says dgProgram name doe not exist in current context.
I restarted my solution but didn't work.
You need to use FindControl on the parent GridView by a row index.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
//bind the datasource for the parent gridview
dgInstitute.DataSource = mySource1;
dgInstitute.DataBind();
//find the nested gridview in row 4 and cast it
GridView nestedGridView = dgInstitute.Rows[3].FindControl("dgProgram") as GridView;
//bind the datasource for the nested gridview
nestedGridView.DataSource = mySource2;
nestedGridView.DataBind();
}
}
Or in the RowDataBound event of the parent GridView.
protected void dgInstitute_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the nested gridview in row 4 and cast it
GridView nestedGridView = e.Row.FindControl("dgProgram") as GridView;
//bind the datasource for the nested gridview
nestedGridView.DataSource = mySource2;
nestedGridView.DataBind();
}
}

To add new row in gridview using javascript

In the below code i have gridview which has textbox and dropdownlist i want to add rows using javascript.My aim is to avoid postback on adding rows.
Markup code:
<asp:GridView runat="server" ID="gvProduct" AutoGenerateColumns="false"
Width="100%" CellPadding="4" ForeColor="#333333" ShowFooter="true"
PageSize-Mode="NumericPages" PagerStyle-Visible="true" AllowPaging="false" AllowSorting="true"
CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
OnRowDataBound="gvProduct_RowDataBound" OnRowCommand="gvProduct_RowCommand" OnRowDeleting="gvProduct_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Product Name" ItemStyle-Width="350px">
<ItemTemplate>
<asp:DropDownList ID="ddlProduct" runat="server" AutoPostBack="false" Style="width: 100%; height:23px" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Current Stock" ItemStyle-Width="80px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblCurrentStock" runat="server" onkeypress="return isNumberKey(event, false);" Height="20px" style="width:80px" Enabled="false" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" ItemStyle-Width="80px">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" onkeypress="return isNumberKey(event, false);" runat="server" Height="20px" Width="150px" onblur="js_function(this);" > </asp:TextBox>
<asp:Label ID="lblunittype" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button2" OnClientClick="AddRow(); return false;" runat="server" Text="Button" />
Javascript code:
function AddRow() {
var table = document.getElementById('<%=gvProduct.ClientID %>');
var newRow = table.insertRow();
var i = 0;
for (i = 0; i < table.rows[0].cells.length; i++) {
var newCell = newRow.insertCell();
newCell.innerHTML = 'New Row';
}
}
Gridview in asp = Table in HTML
new row in Gridview in asp = new row in Table in HTML
Sample JavaScript code:
function AddRow() {
let tableRef = document.getElementById('MainContent_gvItems');
let newRow = tableRef.insertRow(-1);//inserts at last row
let Cell0 = newRow.insertCell(0);
let Text0 = document.createTextNode('mydata');
Cell0.appendChild(Text0);
}
Btw, GridView must be visible even it's empty.
If you just want to add rows to the table for presentation, then #Mostafa Shehata answer should work fine.
However adding rows in JavaScript does not attached it to the GridView datasource. Therefore you'll experience issues with processing the data in the backend (such as saving to database).
Two possible solutions:
Replace GridView with a html table. The data can be populated & updated using a JavaScript calls to a restful API.
Pre-populate the GridView datasource with empty rows.
Data-bind x amount of empty fields (for example 10 empty fields).
In the GridView markup hide all the rows using css.
Run JavaScript to show rows that are not empty.
The “add row” button can just show the first empty row.
Add some sort of notification, if all empty fields have been used. (for example: "please save your data, before continuing").
In code behind, remove all empty fields before consuming it.

How to Get only checkedBox rows from gridView

I have some gridView with checkBox option, I want to get only CheckedRows and make a new GridView with them.
GridView:
<asp:GridView ID="GridViewMediaAndSource" runat="server" DataSourceID="SqlDataSourceMedia"
CssClass="mGrid"
PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
PageSize="15" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="MediaType" HeaderText="Източник" />
<asp:BoundField DataField="mediaName" HeaderText="Име на медията" />
</Columns>
</asp:GridView>
How to make new GridView with only data which was cheked from this view
You didn't provided your code so, I hope this give you a start.
for(int i=0; i< Grid.Rows.Count(); i++)
{
CheckBox check = (CheckBox)Grid.Rows[i]["CheckColumnName"].Controls[0];
if(check.Checked)
{
//do something
}
}

Enabling And Disabling Buttons In GridView

I have added a button column to a gridview. I am populating the gridview with a datable through code then binding the datable to the grid.
I need to now look at the first column of the data and check if the text of the column is "NA" if it is the button in that column has to be disabled.....
How can I accomplish this? I am populating the data from code and the button is preadded to the grid in the markup
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:ButtonField Text="Delete" />
</Columns>
</asp:GridView>
GridView1.DataSource = dt;
GridView1.DataBind();
The best thing to do is implement the OnDataBinding method for a Button in a TemplateColumn.
Eg:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="btnDelete" CommandName="Delete"
Text="Delete" OnDataBinding="btnDelete_DataBinding" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then in your codebehind implement your logic:
protected void btnDelete_DataBinding(object sender, System.EventArgs e)
{
Button btn = (Button)(sender);
btn.Enabled = !Eval("TheFieldInYourDataSourceToCompare").ToString().Equals("NA");
}
The advantage to doing it in this manner over the other posted answers:
No code in your markup
Code is localized to the Button control and can be reused if other Buttons require the same functionality.
Comparing to the DataSource value and not what the visual output is (this could tie into business logic for both rendering and checking).
Hope that helps.
Try this in the DataBound event handler:
protected void GridView1_DataBound(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[1].Text == "NA")
{
// Disable the button
}
}
}
This is just a general idea. You'll have to modify the code for your application.
Don't forget to add OnDataBound="GridView1_DataBound" to the markup for the GridView.
Maybe something like this.
Did the button a little differently
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="Btn_Delete" CommandName="Delete" Text="delete"
Enabled='<%# GridView1.Rows[0].Cells[1].Text != "NA" %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You'll want to you Eval("someColumn") most likely instead of GridView1.Rows[0].Cells[1].Text
Perhaps you can use OnRowDataBound="GridViewRowEventHandler" to set the value to enabled or disabled?
you can try from markup as,
<asp:TemplateField HeaderText="QA signature">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Column6") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Column6") %>' Visible='<%# Eval("Column6") != "" %>' ></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Sign Off" CssClass="cmdButton" Visible='<%# Eval("Column6") == "" %>' />
</ItemTemplate>
</asp:TemplateField>

Categories