I have a grid view which is used to show the Product details. Another thing is there is a table viz. ProductMaster. The data in the gridview is coming from this table. Now what I want is, at first time when page is loaded, only first column should be filled,which contains dropdown list containing product names. All other rows should be empty. Now when user select the Item from the dropdown list, at that time other columns should get filled repectively and other empty row should appear below filled row. Whenever the page is loaded there will be just one empty row(First column dropdown list will be filled with products)
How can I achieve this task??????
Check this article out: http://geekswithblogs.net/dotNETvinz/archive/2009/06/04/adding-dynamic-rows-in-gridview-with-textboxes.aspx
Add DropDownList inside TemplateColumn and in OnRowDataBound event fill it with the product names and instead of Click event of Button as explained in mentioned article you have to handle SelectedIndexChanged event of DropDownList, get the reference on row which contains this DropDownList and fill other columns of this row.
aspx:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="cmbProduct" runat="server" OnSelectedIndexChanged="cmbProduct_Changed" AutoPostBack="true" >
</asp:DropDownList>
</ItemTemplate>
...
</Columns>
code behind:
protected void cmbProduct_Changed(object sender, EventArgs e)
{
DropDownList cmbProduct = (DropDownList)sender;
GridViewRow parentRow = (GridViewRow)cmbProduct.NamingContainer;
string selectedProdId = cmbProduct.SelectedValue;
/* Fetch product details & bind other columns/controls with product details */
Label lbl = (Label)parentRow.FindControl("lblProductName");
lbl.Text = "....";
/* Call AddNewRowToGrid explaied in article*/
AddNewRowToGrid();
}
Related
I added Telerik RadGrid in an ASP.Net project, i want while browsing the page to display multiple empty rows and i want to add data direct to the grid either text or drop down or checkbox columns. I don't want to connect to datasource just empty rows. If cannot be through RadGrid what i can use else.
If all you want are empty rows, create any object that you can iterate over, like a collection, and throw that object into the datasource of the grid. Below is a very basic example.
Example:
<telerik:RadGrid ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" runat="server">
</telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Some Column", typeof(string));
table.Rows.Add(""); // Add some empty rows
table.Rows.Add("");
table.Rows.Add("");
RadGrid1.DataSource = table;
}
I set the sort expression for a column in a grid view. Then sort the column by clicking on Header. Upto this point fine.
However, when I select a gridView row using the select button which is autogenerated:
<asp:GridView runat="server" ID="test" DataSourceID="sourceEmployees"
AutoGenerateSelectButton="true">
After selecting a row if I sort the Column by clicking on Header, the GridView still has the old row selected. The intially selected value is lost. If i Select employeeID value 7, the 7th row remains selected even when I sort the Column in descending order, Although my employeeId value 7 has moved to different row. [ here its moved to 4th row as I have total 10 employees ]
What more I need to implement to make sure No matter the way user sorts the GridView, always the initially selected employeeID remains selected.
You need to handle everything on the code behind (selecting/deselecting the row when the index changes). Here's an example based on your set up:
<asp:GridView DataKeyNames="EmpID"
SelectedRowStyle-BackColor="Yellow" ID="test" OnSelectedIndexChanged="test_SelectedIndexChanged"
runat="server" DataSourceID="sourceEmployees" AutoGenerateColumns="False"
AutoGenerateSelectButton="true" AllowSorting="true"
OnRowDataBound="test_RowDataBound" >
Above, I added two event handlers, one for OnRowDataBound and one for OnSelectedIndexChanged. I also added the DataKey to keep track of the selected employee ID.
Now, on code behind, the for those 2 methods looks like this:
protected void test_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["key"]= test.SelectedDataKey.Value;//Keep track of selected employee by ID
}
protected void test_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var row = e.Row.DataItem as DataRowView;
if (row.Row.Field<int>("EmpID") == (ViewState["key"] != null ? (int)ViewState["key"] : -1))
{
test.SelectedIndex = e.Row.RowIndex;
//Setting the selected Index is not enough, you need to programmatically
//set the color as well. Since I used Yellow on my markup, I use the same here
e.Row.BackColor = System.Drawing.Color.Yellow;
}
}
}
We need to use the GridView.EnablePersistedSelection Property. MSDN states that
If this property is false and a row is selected, the same row is selected when a new page is displayed even though the new page has different data in it. If you set this property to true, when you display a page that has different data in it, no row is selected. If you then return to the page on which a row was selected, that row is still selected.
Setting this property to true resolved my issue.
In a web application I am binding the data to a GridView. In the GridView some of data is repeating. I want to not display the data again and again.
For example Empid is displaying more than one time in the same column. I want to not display the empid again in that column.
You can implement the OnDataBinding event for the specific column you are using. I never use AutoGenerateColumns so having fine control of each cell is pretty simple to implement.
Eg:
// Create global in your .cs file
string _currentEmpID = string.Empty;
Define your column like:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="ltEmpID" runat="server"
OnDataBinding="ltEmpID_DataBinding" />
</ItemTemplate>
</asp:TemplateField>
<!-- Your other columns... -->
</Columns>
Then just implement your DataBinding event:
protected void ltEmpID_DataBinding(object sender, System.EventArgs e)
{
Literal lt = (Literal)(sender);
string empID = Eval("EmpID").ToString();
if (!empID.Equals(_currentEmpID))
{
lt.Text = empID;
_currentEmpID = empID;
}
else
{
lt.Text = string.Empty;
}
}
The RowDataBound forces you to search for controls and if changes are required in the future you have the possibility of breaking other things being modified within the event. Because of this, I prefer to use the control's DataBinding event whenever possible as it localizes functionality to only the control and gives you the flexability to swap out controls and functionality easily without the worry off affecting other things.
If you group your data by the columns you don't want to repeat before binding it to your datasource you can bind an event to RowDataBound and check if the current value equals the previous and then hide the cell.
Check this for an example.
Just add the property AutoGenerateColumns in the gridview and assign it the value of false.
AutoGenerateColumns="false"
I would like to use a TextBoxor Label (item) inside of a Gridview. When field is clicked, I would like to display list of records that contain:
- Item Name
- Description
- Price
- Image
- Add Button
It sounds to me like you are talking about having a "filter" option for your GridView.
If I'm reading your post correctly, you would like to be able to enter text into the TextBox and then filter the data within the GridView to show matching records. Below is some pseudo code that will hopefully help get you started...
FRONT END CODE
<asp:TextBox id="myBox" runat="server" OnTextChange="myBox_OnTextChange"></asp:TextBox>
<asp:GridView id="myGrid" runat="server">
//COLUMN 1
//COLUMN 2
//IMAGE TO ADD
</asp:GridView>
CODE BEHIND
//THIS CODE IS NOT CORRECT EXAMPLE ONLY TO GET YOU STARTED
protected void myBox_OnTextChange(EventArgs e)
{
if(!String.isNullorEmpty(this.myBox.Text))
{
//MyFunction will filter your datasource with the text box data and
//return a DataSet or DataTable or etc.....
this.myGrid.DataSource = MyFunction(myBox.Text);
this.myGrid.DataBind();
}
}
I have a GridView binded to a datasource, i was wondering of how i can check a row cell value (bool) from the database row being bound , and then show a button on the rows where the cell value equals to false..
i am using OndataBound Event to retrieve the Gridview-row being bound, i take the ID show, run another procedure against the database to find the value of the cell from the corresponding Database row..
but i cant figure out how to add the button..
also is there any other ways to handle this scenario?
Solution 1: Create a button with an ID where you want it in the gridview, with an attribute visible=false. Whenever you want to show the button, retrieve it (currentGridRow.FindControl("chosen button ID")) and set it's visible attribute to true.
put your button in a templatefield, like that:
<asp:TemplateField HeaderText="foobar" >
<ItemTemplate>
<asp:ImageButton ID="plusbutton" CssClass="cplusButton" ToolTip="plusButton" OnClick="buttonAdd_Click" runat="server" Visible = "false"/>
</ItemTemplate>
</asp:TemplateField>
Solution 2: dynamically create the button (Button b = new Button; currentGridRow.Cell[].Controls.Add(b);), but it's a pain to deal with the viewstate and the events handler. Don't go that way.