I am trying to update a database using the GridView edit, update CommandField. I have two editable fields which are displayed as text boxes when in edit mode. When clicking submit, I am trying to put the text box values into variables to work with, but I am unable to access them. The two column names are "EOR" and "CategoryName". I have found several suggestions on other forums to try something like:
protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtEor = (TextBox)myGridName.Rows[e.RowIndex].FindControl("EOR");
When I debug the program, txtEor is always null. The only thing i can think of is that I am not referencing the cell properly. I set the Headertext, AccessibleHeaderText, DataField, and SortExpression to "EOR" but it still just comes up null.
Any help would be greatly appreciated!
asp for the gridview:
<asp:GridView ID="grdEOR" runat="server" BackColor="White"
BorderColor="#999999" OnPageIndexChanging="grdEor_PageIndexChanging"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
AllowPaging="True"
PageSize="15" AutoGenerateColumns="False" onrowediting="grdEOR_RowEditing"
onrowcancelingedit="grdEOR_RowCancelingEdit"
onrowupdating="grdEOR_RowUpdating" onrowdeleting="grdEOR_RowDeleting"
ShowFooter="True">
<PagerSettings Mode="NumericFirstLast" />
<Columns>
<asp:BoundField DataField="EORCategoryID" HeaderText="EORCategoryID"
SortExpression="EORCategoryID" ReadOnly="True">
</asp:BoundField>
<asp:BoundField DataField="EOR" HeaderText="EOR" SortExpression="EOR"
AccessibleHeaderText="EOR"/>
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
SortExpression="CategoryName" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True"
ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" BorderColor="Black"
BorderStyle="Solid" BorderWidth="5px" />
</asp:GridView>
I finally found a way that works:
string newEor = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string newCategoryName = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
Related
How to make any column hyperlinked (except first one) in dynamic gridview , where that hyperlink column is linked to another gridview.
This dynamic table shows two type of output as mentioned below:
View 1:
Start Bus_no Change_at Change_Bus_no Destination Means
Desu Office 715 Hanuman Mandir 781 Subroto Park DTC
Desu Office 715 Palam Airport 764 Subroto Park DTC
Desu Office 715 Palam Airport 781 Subroto Park DTC
View 2:
Start bus_no Destination Means
Subroto Park 764 Nehru Place Terminal DTC
In first view I want to make column 2nd (Bus_no) and 4th(Changed_Bus_no) and in second view I want to make column 2nd as hyperlink column which will be linked to another grid view table.
Gridview Code is given below:
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
GridLines="Vertical" ForeColor="Black">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
please help. Thanx in advance.
You can use Template Field for your requirement.
Try like this,
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
GridLines="Vertical" ForeColor="Black">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
<Columns>
<asp:TemplateField HeaderText="First Column">
<ItemTemplate>
<asp:Label ID="lblthird0" runat="server" Text="items" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Second Column">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="#Gridview2"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can do like this for all your columns.
I have a gridview in aspx page, I need it to go add hyperlink to the Component from BoundField once the user clicks on the Component1 value. How can I add the hyperlink to boundfield that's related to BoundField?
<asp:GridView ID="Module" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="dsrcGetModuleData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
<FooterStyle BackColor="#CCCC99" />
<Columns>
<asp:BoundField DataField="Component1" ItemStyle-Font-Size="Small" HeaderStyle-Width="80px" HeaderStyle-Font-Size ="Medium" SortExpression="Component1" />
</Columns>
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Try this.
<asp:GridView ID="Module" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="dsrcGetModuleData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
<FooterStyle BackColor="#CCCC99" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="StockNumber" HeaderText="Stock Number" DataNavigateUrlFormatString="ReplacePage.aspx?StockNumber={0}" DataTextField="StockNumber" />
</Columns>
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
To pass more than one variable, do this.
<asp:GridView ID="Module" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="dsrcGetModuleData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
<FooterStyle BackColor="#CCCC99" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="StockNumber, ID, CITY" HeaderText="Stock Number" DataNavigateUrlFormatString="ReplacePage.aspx?StockNumber={0}&id={1}&CITY{2}" DataTextField="StockNumber" />
</Columns>
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
DataNavigateUrlFields - The fields you would like to pass using the hyperlink column.
DataTextField - Current display field in the DatagridView.
HeaderText - header text which should be the description of the DataTextField value.
You won't be able to have a linkbutton in a bound field. However you can convert it to a TemplateField. Here is an example of my LinkButton.
<asp:TemplateField HeaderText="StockNumber" SortExpression="STOCK NO">
<ItemTemplate>
<asp:LinkButton ID="lbStockNumber" runat="server" Text='<%# Bind("StockNumber") %>' OnClick="lbStockNumber_Click"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle BackColor="Black" ForeColor="White" HorizontalAlign="Left" Width="80px" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
To convert it to a TemplateField. Make sure you are in Design View. Then click on the smart tag. Edit Columns, select your column, then below the properties click "Convert to TemplateField"
EDIT: I just noticed you wanted a HyperLink instead of a LinkButton. You will still convert it the same way, but just put a HyperLink instead.
<asp:HyperLink ID="hlStockNumber" runat="server" Text='<%# Bind("StockNumber") %>' OnClick="lbStockNumber_Click"></asp:HyperLink>
Hope this helps!
I have a gridview (default) with select buttons. Is it possible to get on which button was clicked in event onInit() ?
I tried to use
Request["__EVENTTARGET"];
but this returns only "GridView1". Thanks you for the answers
EDIT
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="gridView" DataKeyNames="Nazev" DataSourceID="OblastiSource" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="Nazev" HeaderText="Nazev" ReadOnly="True" SortExpression="Nazev" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" ButtonType="Button" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
You should know, that DataField="Nazev" is the number I spoke about. In reality there is a M:N relation behind (I wanted to keep it simple for you).
I have two tables: "Towns" & " Streets". The gridview1 shows the Towns names. When the user select a town, I want to generate checkboxes with "street names". First the checked and add to Panel1 and then the unchecked and add to Panel2. Checked means, that the street is in the town.
So the town have lot of streets and one street have lot of towns (M:N relation). Do you have some smoother solution? I want to keep this structure.
I am pretty much sure that this question have been asked and answered several times before. But I am asking it again for an alternate answer. Here is my GridView
<asp:GridView ID="dgvGeneralBillList" runat="server" style="font-size:11px;margin:10px auto auto 30px;width:500px;" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" OnSelectedIndexChanged="dgvGeneralBillList_SelectedIndexChanged">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="BillID" HeaderText="Bill ID" Visible="False" />
<asp:BoundField DataField="SerialNo" HeaderText="Bill No" />
<asp:BoundField DataField="BilledWeekNo" HeaderText="Billed Week" />
<asp:BoundField DataField="BilledWeekDate" HeaderText="Billed Date" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
<asp:BoundField DataField="BillStatus" HeaderText="Bill Status" />
<asp:CommandField SelectText="Print" ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
Now I want to add rows to this gridview in runtime. In other articles, they suggest to define a DataTable, then ADD my desired COLUMNS to that table, then ADD rows in that TABLE and finally BIND the table to the GridView. Yes, This solves the problem and I can also do that. But what I want to do is, as I already have columns added into my gridview, I just want to add rows in it. and NOT Define a DataTable and then Bind it to GridView stuff. I tried below,
oOutputBill = (clsBill[])oOutput;
if (oOutputBill.Length > 0)
{
for (int i = 0; i < oOutputBill.Length; i++)
{
GridViewRow oRow = new GridViewRow();
oRow.Cells[0].Text = Convert.ToString(oOutputBill[i].BillID);
oRow.Cells[1].Text = Convert.ToString(oOutputBill[i].SerialNo);
oRow.Cells[2].Text = Convert.ToString(oOutputBill[i].BilledWeekNo);
oRow.Cells[3].Text = Convert.ToString(oOutputBill[i].BilledWeekDate);
oRow.Cells[4].Text = Convert.ToString(oOutputBill[i].Amount);
oRow.Cells[5].Text = Convert.ToString(oOutputBill[i].BillStatus);
}
}
and as expected, it gives the error "No Overload of Method GridViewRow takes 0 arguments". How can I achieve this? Thanks in advance.
This link may be helpful.
But I recommend to bind GridView by DataSource.
Just save your data into session and instead of manually adding rows into GridView, add new row into saved data and rebind your grid.
Example:
// If you have List of clsBill.
List<clsBill> oOutputBill = 'Filled from DB...';
// Save Data into session
Session["oOutputBill"] = oOutputBill;
// Bind your GridView
dgvGeneralBillList.DataSource = Session["oOutputBill"];
dgvGeneralBillList.DataBind();
...
// Get saved data and insert new row
List<clsBill> oOutputBill = Session["oOutputBill"] as List<clsBill>;
if(oOutputBill != null)
{
oOutputBill.Add(new clsBill() { /* Fill class properties */ } );
// Rebind grid
dgvGeneralBillList.DataSource = Session["oOutputBill"];
dgvGeneralBillList.DataBind();
}
This is what I am using now as solution.
My GridView is-
<asp:GridView ID="dgvGeneralBillList" runat="server" style="font-size:11px;margin:10px auto auto 30px;width:auto;" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" OnSelectedIndexChanged="dgvGeneralBillList_SelectedIndexChanged">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="BillID" HeaderText="Bill ID" />
<asp:BoundField DataField="SerialNo" HeaderText="Bill No" />
<asp:BoundField DataField="BilledWeekNo" HeaderText="Billed Week" />
<asp:BoundField DataField="BilledWeekDate" HeaderText="Billed Date" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
<asp:BoundField DataField="BillStatus" HeaderText="Bill Status" />
<asp:CommandField SelectText="Print" ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
And the way of adding row is-
oOutputBill = (clsBill[])oOutput;
if (oOutputBill.Length > 0)
{
dgvGeneralBillList.DataSource = oOutputBill;
dgvGeneralBillList.DataBind();
dgvGeneralBillList.Visible = true;
}
This answer is similar to define a table, add columns to table, add rows to table and finally bind the table to gridview but with less code. in this way, only columns defined in the gridview are bound with the data source. Please note that, if you want to do something similar to OnSelectedIndexChanged and try to get any data from the gridview, the DataColumn HAVE TO be DEFINED and VISIBLE in the gridview. But if anyone can provide me the sample code as my question, it would be highly appreciated.
This is May Sample Code in ASP.NET How Can I Add A similar Image Column To All Rows After Binding
gridview1.DataSource = MB.GetTest();
gridview1.DataBind();
And My ASPX Page:
<asp:GridView ID="gridview1" runat="server">
</asp:GridView>
I really need irt, thanks.
Update
<asp:GridView ID="gridview1" runat="server">
<Columns>
<asp:ImageField>
</asp:ImageField>
</Columns>
</asp:GridView>
hi i would like to explain you a little there is a property called AutoGenerateColumns="false" you need to set this as false
and generate columns in your aspx and bind them Refer this Site
<asp:GridView ID="GridView1" Runat="server"
DataSource='<%# GetData() %>' AutoGenerateColumns="False"
BorderWidth="1px" BackColor="White" CellPadding="3" BorderStyle="None"
BorderColor="#CCCCCC" Font-Names="Arial">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<PagerStyle ForeColor="#000066" HorizontalAlign="Left"
BackColor="White"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#006699"></HeaderStyle>
<Columns>
<asp:BoundField HeaderText="Picutre ID" DataField="PictureID">
<ItemStyle HorizontalAlign="Center"
VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Title" DataField="Title"></asp:BoundField>
<asp:BoundField HeaderText="Date Added" DataField="DateAdded"
DataFormatString="{0:d}">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:ImageField DataImageUrlField="PictureURL"></asp:ImageField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
BackColor="#669999"></SelectedRowStyle>
<RowStyle ForeColor="#000066"></RowStyle>
</asp:GridView>
There you go with Image column above
<asp:ImageField DataImageUrlField="PictureURL"></asp:ImageField>
MSDN is another best site to refer...
above code is taken from MSDN website.