I'm using a for loop to add columns to the DevExpress ASP.Net WebForms GridView using VB.Net. I was able to get a hyperlink to reference the same column value:
Dim newColumn As New DevExpress.Web.GridViewDataHyperLinkColumn
newColumn.PropertiesHyperLinkEdit.NavigateUrlFormatString = "TrendView.aspx?CurrentID={0}"
I need to programmatically set the hyperlink to another column's value... i.e. column three needs to have a hyperlink that references the column 1 value in the same row. How do you access another column in that row using VB or C# during runtime?
please refer this url to solve your problem
https://www.devexpress.com/Support/Center/Example/Details/E308
change your populate grid logic as
ASPX :
<dx:ASPxGridView ID="ASPxGridView1" runat="server"></dx:ASPxGridView>
CS
protected void Page_Init(object sender, EventArgs e)
{
ASPxGridView1.KeyFieldName = "ID";
ASPxGridView1.DataSource = GetData();
if (!IsPostBack && !IsCallback)
{
PopulateColumns();
ASPxGridView1.DataBind();
}
}
public DataTable GetData()
{
DataTable Table = new DataTable();
Table.Columns.Add("ID", typeof(int));
Table.Columns.Add("ItemName", typeof(string));
Table.Columns.Add("ItemValue", typeof(string));
Table.Rows.Add(1, "A","AA");
Table.Rows.Add(2, "B","BB");
return Table;
}
public void PopulateColumns()
{
GridViewDataTextColumn colID = new GridViewDataTextColumn();
colID.FieldName = "ID";
ASPxGridView1.Columns.Add(colID);
GridViewDataTextColumn srk = new GridViewDataTextColumn();
srk.FieldName = "ItemValue";
ASPxGridView1.Columns.Add(srk);
GridViewDataHyperLinkColumn colItemName = new GridViewDataHyperLinkColumn();
colItemName.FieldName = "ItemValue";
colItemName.PropertiesHyperLinkEdit.NavigateUrlFormatString = "~/details.aspx?Device={0}";
colItemName.PropertiesHyperLinkEdit.TextFormatString = "{0}";
colItemName.PropertiesHyperLinkEdit.TextField = "ItemName";
ASPxGridView1.Columns.Add(colItemName);
}
here column itemName refer to itemValue as url string params
If you want to show hyperlink text based on multiple columns then HyperlinkColumn is not the right approach to implement. It would be better use templates.
You should create DataItemTemplate for the column and there you can use Bind statement to format the display text or Hyperlink url as you want. It is the same approach used in ASP.NET GridView control and work in similar manner with ASPxGridView control.
I suggest you to go through these example and attached sample projects will help you know about the implementation.
ASPxGridView - How to customize HyperLink column
How to use a hyperlink whose argument depends on several cell values in the ASPxGridView
How to customize navigate URL for HyperLink column within a ASPxGridView - This contains sample attached in the answer.
Example: DataItemTemplate for column.
<dxwgv:GridViewDataTextColumn FieldName="ContactName" VisibleIndex="3">
<DataItemTemplate>
<dxe:ASPxComboBox ID="ASPxComboBox1" runat="server" ValueType="System.String" DataSourceID="AccessDataSource1"
TextField="ContactName" ValueField="ContactName" Value='<%#Bind("ContactName")%>' OnSelectedIndexChanged="ASPxComboBox1_SelectedIndexChanged">
<ClientSideEvents SelectedIndexChanged="onSelectedIndexChanged" />
</dxe:ASPxComboBox>
</DataItemTemplate>
</dxwgv:GridViewDataTextColumn>
or
<dx:ASPxHyperLink ID="ASPxHyperLink3" runat="server"
NavigateUrl='<%# string.Format("~/AccountDetail.aspx?CategoryID={0}", Eval("i_Customer")) %>'
Text='<%# string.Format("i_Customer{0}", Eval("i_Customer")) %>' .../>
ASPxGridView - ASPxHyperLink Navigate URL formatting
ASPxGridView - How to set GridViewDataHyperlinkColumn's text and navigate url
Related
IN dot net, User control has a drop down when it is changed. The grid needs to be changed, but the grid is not inside user control?
How can we achieve it?
You bind an event using the OnSelectedIndexChange to your drop down list on your aspx page:
<asp:DropDownList ID="ddlGridType" runat="server" OnSelectedIndexChanged="ddlGridType_SelectedIndexChanged" AutoPostBack="true" >
Then on your C# code behind:
protected void ddlRunType_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtblDataSource = New DataTable();
string gridType = ddlGridType.SelectedValue;
//remove the old data from the grid view
TheGridView.DataSource = null;
TheGridView.DataBind();
//get the new data using whatever method you use to get the data
dtblDataSource = theGridView_GetDataSource(gridType);
//bind the new data to the list
TheGridView.DataSource = dtblDataSource;
TheGridView.DataBind();
}
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;
}
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 need to hide /unhide the labels and textbox depending on db results , I tried something like this but it doesnt works, the condition should be like if the db field is empty for that field , then the label associated with that field should hidden (not visible) , following is the code i tried :
<asp:Label ID="lblBirth" Text="DOB:" runat="server" ViewStateMode="Disabled" CssClass="lbl" />
<asp:Label ID="DOB" runat="server" CssClass="lblResult" Visible='<%# Eval("Berth") == DBNull.Value %>'></asp:Label>
Code behind:
protected void showDetails(int makeID)
{// get all the details of the selected caravan and populate the empty fields
DataTable dt = new DataTable();
DataTableReader dtr = caravans.GetCaravanDetailsByMakeID(makeID);
while (dtr.Read())
{
//spec
string value = dtr["Price"].ToString();
lblModel.Text = dtr["model"].ToString();
birthResult.Text = dtr["Berth"].ToString(); }}
To make your aspx version work your control should be data bound to data source that contains "Berth" property. As I can see from code behind, you prefer to use c# to populate controls. In this case you may just do the following:
DOB.Visible = dtr["Berth"] == DBNull.Value;
I think that using data binding is more preferable solution.
The situation is as follows:
I have a database with many RSS Categories, that in turn have many RSS Feeds. I want to display them in the following fashion:
RSS Category 1
[ ] Rss Feed 1
[ ] Rss Feed 2
[ ] Rss Feed 3
RSS Category 2
[ ] Rss Feed 1
[ ] Rss Feed 2
[ ] Rss Feed 3
Where [ ] represents a checkbox.
So each RSS Feed is pulled out of the database depending on the id of the parent RSS category. I cannot use CheckBoxList as each checkbox must be contained inside an unordered list list item. I need the markup to be semantically correct and cant use the Control Adapters.
I initially imagined two nested repeaters, the outside one databound to a list of RSS Categories from the database which displays the Category header and contains a hidden control with the Category ID, then an inner Repeater with the RSS Feeds for that category.
How do I access the Category id from the hidden field control in the parent repeater so I can look up the correct RSS Feeds?
I think you might be looking for something like this:
ASPX:
<asp:Repeater ID="rptOuter" runat="server" OnItemDataBound="rptOuter_ItemDataBound">
<HeaderTemplate>
<ul style="list-style-type: none">
</HeaderTemplate>
<ItemTemplate>
<li id='<%# Eval("Id") %>'>
<%# Eval("Category") %>
<asp:Repeater ID="rptInner" runat="server">
<HeaderTemplate>
<ul style="list-style-type: none">
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:CheckBox ID="chkFeed" runat="server" Text='<%# Eval("Feed") %>' />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT id, category FROM rsscategory_tbl", conn);
SqlDataReader rdr = cmd.ExecuteReader();
this.rptOuter.DataSource = rdr;
this.rptOuter.DataBind();
rdr.Close();
}
}
protected void rptOuter_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
System.Data.Common.DbDataRecord rd = (System.Data.Common.DbDataRecord)e.Item.DataItem;
SqlCommand cmd = new SqlCommand("SELECT feed FROM rssfeed_tbl WHERE categoryid = " + rd.GetInt32(rd.GetOrdinal("id")), conn);
Repeater rptInner = (Repeater)e.Item.FindControl("rptInner");
SqlDataReader rdr = cmd.ExecuteReader();
rptInner.DataSource = rdr;
rptInner.DataBind();
rdr.Close();
}
}
}
If you can't use a CheckBoxList, you'll have to handle the values manually.
You can manually .Controls.Add(new LiteralControl("</li><li>")) between them, but that's a bit on the tacky side.
You could use a DataGrid with a TemplateColumn to hack your way into it, but that's not elegant.
Why does this need to be in a list? Doesn't the CheckBoxList provide the functionality you require?
Also, try myCheckBox.Parent.Controls.FindControl("MyHiddenField").Value .
How do I access the Category id from the hidden field control in the parent repeater so I can look up the correct RSS Feeds?
I have an application where I need to dynamically create a whole variety of different controls. It works fine for multiple checkboxes (or anything else for that matter).
First, on your ASPX page, place the "PlaceHolder" control:
<asp:PlaceHolder runat="server" id="CtrlCanvas" />
Now, in your code behind, create your controls dynamically:
Label aLbl = new Label {Text = "Prompt: ", ID = ("WSLabel" + counter++)};
CtrlCanvas.Controls.Add(aLbl);
That was a label, here is a TextBox:
TextBox aBox = new TextBox {ID = "XTest1", Columns = 5, Text = " ", Width = 50};
CtrlCanvas.Controls.Add(aBox);
aBox.Focus();
Here is a radio box list:
RadioButtonList WRRadio = new RadioButtonList { ID = "XTestRadioList1" };
WRRadio.Items.Add("Walking ");
WRRadio.Items.Add("Running");
WRRadio.SelectedIndex = WalkRun;
WRRadio.RepeatColumns = 2;
CtrlCanvas.Controls.Add(WRRadio);
That is all there is to creating the control. To get the values, you'll need to have a naming convention that allows you to identify the prefix that ASP.NET puts on your controls. I use "XTest". Here is an example:
foreach (string aStr in Form.AllKeys)
{
int position = aStr.IndexOf("XTest"); // XTest is used in my controls
if (position > 0)
{
// Get the *prefix* added by ASP.NET
CtlPrefix = aStr.Substring(0, position);
break;
}
}
Now to get the value stored in a specific control it is easy:
string result = Form.Get(CtlPrefix + "XTest1")
For radio boxes, at least, the Get returns the selected index. I imagine that you'll name each check box separately so you can just check for 0 or 1.
Hope this helps!
I need the markup to be semantically correct and cant use the Control Adapters.
Are you referring to these adapters? If not, you should take a look. You don't have to use the entire set; Edit your .browser file to specify only the CheckBoxListAdapter. It should do exactly what you need. This way you can simply take a Repeater or ListView control and databind it to the list of categories, then put a CheckBoxList inside that.