I have SqlDataSource on my ASP.NET page and load this into Telerik's RadGrid control.
<asp:SqlDataSource
ID="DataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:ServerConnectionString %>"
SelectCommand="SELECT [Timestamp], [Label], [Project], [Product], [Value], [Reference] FROM [Operation]">
</asp:SqlDataSource
My field [Value] is a byte value representing an enum. When I load data into the grid I would like to convert these byte values into their enum equivalents, in other words I want clear text form.
I tried to do something in OnSelected and OnLoad() events of the SqlDataSource component but no success. Is there a way to access data in code behind coming from SqlDataSource before it is rendered?
You can look at the ItemDataBound event of the RadGrid which is similar to RowDataBound event in GridView. For example:
ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="DataSource"
OnItemDataBound="RadGrid1_ItemDataBound" >
C# Code-Behind:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem) // get the row collection of cells/items
{
GridDataItem dataItem = (GridDataItem)e.Item;
// here we are converting the TEXT value to INT of the "VALUE" column
if(int.Parse(dataItem["Value"].Text) == 1)
dataItem["Value"].Text = "A"; // here you can set cell value as you like
}
}
Related
How to retrieve the first value displayed on the DevExpress' ASPxCombobox connecting to a SQL data source? After the page first loads, if I use selectedItem to retrieve the first value displayed on the ASPxCombo box without clicking on the combobox, I will get "null" for selectedItem. When I click and choose a value from a combobox, I can use selectedItem to get that value.
To access elements in dropdown window of ASPxComboBox use its Items collection:
//retrieve value of the first element in dropdown window of ASPxComboBox
var firstItem = comboBox.Items[0].Value
Combobox selecteditem would not be available on Page Load. Data isn't bound to control at this point, instead use DataBound event.
Webform1.aspx
<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" DataSourceID="SqlDataSource2" TextField="CategoryName" ValueField="CategoryID">
</dx:ASPxComboBox>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:NWindConnectionString %>"
SelectCommand="SELECT * FROM [Categories]">
</asp:SqlDataSource>
Webform1.aspx.cs
void ASPxComboBox1_DataBound(object sender, EventArgs e)
{
ASPxComboBox1.SelectedIndex = 0;
object selectedItem = ASPxComboBox1.SelectedItem;
string selectedValue = ASPxComboBox1.SelectedItem.Value.ToString();
}
Write this in your Page_Load:
if (!IsPostBack)
{
cmb.DataBind();
cmb.SelectedIndex = 0;
}
If you want to get the first item in the combobox then use:
ASPxComboBox1.Items[0]
If you want to get the selected value then use:
ASPxComboBox1.Value
I'm trying to populate a single text box (or parameter) with data from a gridview column when I click on a button in that row.
Gridview gets it data from a sqlconnection
the gridview is
| Drawing |
| 12345 | VIEW
| 12346 | VIEW
the VIEW is a template button with an onclick event, when the user clicks the button the data from the Drawing column (12345) should be passed to ether a textbox or a paremeter. (this is the part I dont know how to do) once the Iv got the number in a textbox I can use it as pareameter and then a pdf is opened of that drawing, I have code for this and is working.
thanks for any help
If you are using C#, the simplest thing to do would be to add an in-built select command button to the gridview rows at runtime. Then on the selectedindexchanged event of the gridview simply access the cell of the selected row that you want the value from. You can then assign that string to anything you want. Like so:
protected void myGridView_SelectedIndexChanged(object sender, EventArgs e)
{
string myString = myGridView.SelectedRow.Cells[4].Text.ToString();
TextBox1.Text = myString;
}
Remember that the cell index collection is zero based, so [0] is actually the first cell in the row.
Use TemplateFields and the grid view's OnRowCommand event, like this:
Markup:
<asp:gridview id="GridView1"
OnRowCommand="GridView1_RowCommand"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBoxDrawing" runat="server"
Text="<%# Eval("Drawing")) %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="selc" runat="server" Text="View"
CommandName="View"
CommandArgument="<%# ((GridViewRow)Container).RowIndex %> />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked
if(e.CommandName == "View")
{
// Convert the row index stored in the CommandArgument
// property to an integer
var index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection
var row = GridView1.Rows[index];
// Find the drawing value
var theDrawingTextBox = row.FindControl("TextBoxDrawing") as TextBox;
// Verify the text box exists before we try to use it
if(theDrawingTextBox != null)
{
var theDrawingValue = theDrawingTextBox.Text;
// Do something here with drawing value
}
}
}
Here is my aspx code:
<EditItemTemplate>
<asp:DropDownList ID="ddlTotalColumn" runat="server">
<asp:ListItem Value="">Select value</asp:ListItem>
<asp:ListItem Value="0">1</asp:ListItem>
<asp:ListItem Value="1">2</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
My aspx.cs code:
protected void gvTest_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow selected_row = gvTest.Rows[e.RowIndex];
var total_column_drop_down_list = (DropDownList)selected_row.FindControl("ddlTotalColumn");
int column_string = Convert.ToInt32(total_column_drop_down_list.SelectedItem.Value);
gvTest.EditIndex = -1;
...
}
At this line: int column_string = Convert.ToInt32(total_column_drop_down_list.SelectedItem.Value);
I have an error: "Input string was in incorrect format" because "total_column_drop_down_list.SelectedItem.Value" will return empty string ("").
So is there any bright idea?
It sounds like you have made the classic mistake of not putting your databinding code inside a if (!Page.IsPostBack) block. Thus, your GridView re-binds, and you get default values in your RowUpdating event (rather than what you selected).
Wherever you are binding your GridView, in Page_Load for instance, you need to do this:
if (!Page.IsPostBack)
{
BindGrid();
}
Where "BindGrid()" is whatever code you call to databind your GridView.
On a slightly unrelated note, you can actually use the GridViewUpdateEventArgs parameter that's passed to that method to grab the updated values (rather than using FindControl to get the DropDownList, and then getting the values).
When editing a row in a GridView, I need a DropDownList whose list of available values depends on other column(s) in the row (let's just say the "type" of the record for now); that is, different options will be listed depending on which row is being edited.
I got it working after a fashion by adding an otherwise-unneeded DataKeyName to the GridView, but this is causing me grief elsewhere and anyway it feels too circuitous, so I'm looking for a better way. Here's how I'm currently doing it:
In .aspx file:
<asp:GridView ID="gvDct" ... DataKeyNames="dctId,dctType" ... >
...
<asp:TemplateField>
...
<EditItemTemplate>
<asp:DropDownList ID="ddlDctParent" runat="server"
DataSourceId="sdsDctParent"
DataValueField="dctId" DataTextField="dctFullPath"
SelectedValue='<%# Bind("dctParentId") %>'
... >
</asp:DropDownList>
<asp:SqlDataSource ID="sdsDctParent" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
OnSelecting="sdsDctParent_Selecting"
SelectCommand="SELECT dctId, dctFullPath FROM lkpDocCatAndType
WHERE dctType=#dctType">
<SelectParameters>
<asp:Parameter Name="dctType" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
...
</asp:GridView>
I wanted the SelectParameter to be a ControlParameter with ControlID="gvDct" and PropertyName="SelectedDataKey.Values[dctType]", but for unknown reasons that didn't work (the parameter values were null), so I added this bit to the .vb code-behind file to populate the row-specific parameter for the data source of the DropDownList:
Protected Sub sdsDctParent_Selecting(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
e.Command.Parameters(0).Value = gvDct.DataKeys(gvDct.EditIndex).Item(1)
End Sub
So, when I start editing a row in the GridView, the DropDownList is bound, which causes the SqlDataSource SelectCommand to execute, which fires the OnSelecting code where I provide the parameter value from the row being edited (EditIndex) so that I get the appropriate population of values in the DropDownList.
Is there a "better" way? The most important aspect for me is to avoid adding the DataKeyName, because that is causing me a too-many-parameters problem on the stored procedure I'm using to delete a row from the GridView.
You should use Gridview RowDataBound where you can fetch the valueID a later on you can populate your Dropdownlist also add a label/hidden field whose value you want to populate DropdownList
Sry for writing code in c#.
You can try something like this
protected void gvDct_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
Label lblValue = (Label)e.Row.FindControl("YourLableID")
DropDownList ddList= (DropDownList)e.Row.FindControl("ddlDctParent");
//bind dropdownlist
DataTable dt = con.GetData("Select Query where YourColumn='"+lblValue.text+"'");
ddList.DataSource = dt;
ddList.DataTextField = "dctFullPath";
ddList.DataValueField = "dctId";
ddList.DataBind();
DataRowView dr = e.Row.DataItem as DataRowView;
ddList.SelectedValue = dr["dctId"].ToString();
}
}
}
the reason why i am looking to update dynamic is because i am using objectdatasource and my objectdatasource have a collection of object and within that object i have another object that i wanted to access so for an example:
+Student
......
......
......
-Courses
.........
.........
Name
Update end
how do i bind templatefield from code-behind?
<asp:Gridview ID="gridview1" runat="Server">
<columns>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:Gridview>
First of all define your key field in GridView control, just add net attribute to GridView markup: datakeynames="StudentID".
You can use both event handler for GridView: RowDataBound or RowCreated. Just add one of this event handler and find there control that is placed in your ItemTemplate. Like here, for instance:
void ProductsGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
Label someLabel = (Label)e.Row.FindControl("someLabel");
if (someLabel != null)
{
// Get Student index
int StudentId = (int)GridView.DataKeys[e.Row.RowIndex].Values[0];
// Set the Label Text
// Define here all the courses regarding to current student id
someLabel.Text = //
}
}
}
This example was gotten from MSDN
Here are some code samples from MSDN:
http://msdn.microsoft.com/en-us/library/aa479353.aspx
These are in VB but you should be able to locate C# also :-)
If you follow this link and scroll down you will find a code sample:
http://bytes.com/topic/asp-net/answers/624380-gridview-generated-programmatically