|
Hi guys,
Anybody knows how to add a column to a datagrid?
I want to create a datagrid for database search result. However, I want to check on something with the data and show yes/no in the additional column in the datagrid according to some conditions.
Thank you
|
|
|
|
|
Probably the simplest way to handle this is to add a <TemplateColumn> to your DataGrid's <Columns> collection. You can use a custom function call within the context of a databinding epxression to display "yes" or "no" based on data in the row.
|
|
|
|
|
How do you do that? Sorry, I'm a beginner here.
Thank you
|
|
|
|
|
Well, you have to know how to work with a TemplateColumn in a DataGrid. If you Google for something like "TemplateColumn DataGrid" you'll get lots of hits to articles that will help. Here's one:
http://aspnet.4guysfromrolla.com/articles/061002-1.aspx[^]
You'll notice in the article the use of the DataBinder.Eval() expression syntax. This syntax allows for data values to be evaluated and displayed in the template column. The article shows a good example of this.
Now in your case, you want a custom function that will determine whether a "yes" or a "no" should be outputted in the additional column. For the sake of example, let's say you have a field called "TotalSales" and you want to have a column labeled "Top Seller" show "yes" when the TotalSales value is greater than 1000. Otherwise, the column shows "no". So you might create a function in your script block that looks like this:
string DisplayTopSeller(int totalSales)
{
if (totalSales > 1000)
return "yes";
else
return "no";
} Then, when defining your DataGrid, you would add a <TemplateColumn> in the <Columns> tag and use an expression that calls the DisplayTopSeller function, passing it the value of the TotalSales field:
<asp:datagrid id="myGrid" runat="server"
AutoGenerateColumns="False">
<Columns>
. . . whatever columns come first . . .
<asp:TemplateColumn HeaderText="Top Seller">
<ItemTemplate>
<%# DisplayTopSeller(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "TotalSales"))) %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid> If you are using .NET 2.0, you can substitute the shorthand Eval("TotalSales") syntax for the longer DataBinder.Eval(Container.DataItem,"TotalSales") . So in 2.0, your databinding expression could look like this:
<%# DisplayTopSeller(Convert.ToInt32(Eval("TotalSales"))) %>
I hope this is helpful.
|
|
|
|
|
Hi,
I have created WSE 3.0 MTOM enabled asp.net 2.0 WebService. My client(Java
Client) which has MTOM enabled on his side is sending the following soap
message. Basically he is sending the soap message with MIME attachment.
------------------------------------------------------------------------------------------------
MIME-Version: 1.0
Content-Type: Multipart/Related;boundary=MIME_boundary;
type="application/xop+xml";
start="<mymessage.xml@ someexample.org="">";
startinfo="application/soap+xml; action=\"ProcessData\""
Content-Description: A SOAP message with a photo and document in it
--MIME_boundary
Content-Type: application/xop+xml;
charset=UTF-8;
type="application/soap+xml; action=\"ProcessData\""
Content-Transfer-Encoding: 8bit
Content-ID: <mymessage....@someexample.org>
<soap:envelope
="" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xmlmime="http://www.w3.org/2004/11/xmlmime">
<soap:body>
<m:data xmlns:m="http:// someexample.org/stuff">
<m:photo><xop:include
="" xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:http:// someexample.org/photo.jpeg">
<m:doc>
|
|
|
|
|
Hello,
I need to get a value which might be on a SQL database or Access database.
My problem is I need to know which is the database type or, even better, create a code which would work for both databases.
Anyway, first of all I am getting my connection string from Web.Config:
Dim connectionString As String = ConfigurationManager.ConnectionStrings("Conn").ConnectionString
Then, if the database is SQL I have the code:
' Define connection
Dim connection As New SqlClient.SqlConnection(connectionString)
' Define command
Dim command As New SqlClient.SqlCommand
With command
.CommandText = "GetValue"
.Connection = connection
.CommandType = CommandType.StoredProcedure
End With
' Add command parameters
With command.Parameters
.Add(New SqlClient.SqlParameter("@Culture", culture))
End With
' Execute the command
connection.Open()
Dim content As SqlClient.SqlDataReader = command.ExecuteReader
If content.Read Then
Return content.Item("Value").ToString()
Else
Return "# Undefined Value#"
End If
content.Close()
connection.Close()
If the database is Access there are a few changes just in types as follows:
' Define connection
Dim connection As New OleDb.OleDbConnection(Me.ConnectionString)
' Define command
Dim command As New OleDb.OleDbCommand
With command
.CommandText = "GetValue"
.Connection = connection
.CommandType = CommandType.StoredProcedure
End With
' Add command parameters
With command.Parameters
.Add(New OleDb.OleDbParameter("@ContentCulture", culture))
End With
' Execute the command
connection.Open()
Dim content As OleDb.OleDbDataReader = command.ExecuteReader
If content.Read Then
Return content.Item("Value").ToString()
Else
Return "# Undefined Value #"
End If
content.Close()
connection.Close()
How should I create this?
Thanks,
Miguel
|
|
|
|
|
Have a look at the DbProviderFactory Class:
http://msdn2.microsoft.com/en-us/library/system.data.common.dbproviderfactory.aspx
http://msdn2.microsoft.com/en-us/library/dd0w4a2z.aspx
|
|
|
|
|
I get a example of FixedDataGrid Written by Alexander Zeitler.His example work well.It is fixed datagrid row height.I want to add some other function to it,such as highlight entire row,The highlight row work well,but the delete ,edit ,update command work failed!
the all code is here,Can anyone give some help?
using System;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//using CustomControls;
namespace WinDataGrid
{
public class FixedHeightGrid : System.Web.UI.WebControls.DataGrid
{
private bool _mergePaddingColumns;
private TableItemStyle _paddingItemStyle = new TableItemStyle();
private int _RowHeight = 22;
private string _sRowForColor = "white";
private string _sRowBackColor = "lightblue";
public FixedHeightGrid()
{
this.ShowFooter=false;
this.ShowHeader=true;
this.AllowPaging=true;
}
public bool MergePaddingColumns
{
get {return _mergePaddingColumns;}
set {_mergePaddingColumns = value;}
}
public TableItemStyle PaddingItemStyle
{
get {return _paddingItemStyle;}
set {_paddingItemStyle = value;}
}
public int RowHeight
{
get { return _RowHeight;}
set {_RowHeight = value;}
}
public string RowForColor
{
get { return _sRowForColor;}
set {_sRowForColor = value;}
}
public string RowBackColor
{
get { return _sRowBackColor;}
set {_sRowBackColor = value;}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
AddPaddingItems();
}
protected override void OnItemCreated(DataGridItemEventArgs e)
{
base.OnItemCreated(e);
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
e.Item.Attributes.Add("OnMouseOver", "this.style.backgroundColor = '" + _sRowBackColor + "';");
e.Item.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '" + _sRowForColor + "';");
}
}
private void AddPaddingItems()
{
int indexCount;
int indexStop = this.PageSize;
int indexStart = 1;
if(this.AllowPaging==true)
{
indexStart++;
indexStop++;
}
Table myTable = (Table)this.Controls[0];
for(indexCount=indexStart+this.Items.Count;indexCount<=indexStop;indexCount++)
{
myTable.Controls.AddAt(indexCount,PaddingItem());
}
}
private DataGridItem PaddingItem()
{
DataGridItem myItem = new DataGridItem(0,0,ListItemType.Item);
Table myTable = (Table)this.Controls[0];
int numberOfColumns = myTable.Rows[1].Cells.Count;
if(this.MergePaddingColumns==true)
{
TableCell myCell = new TableCell();
myCell.ColumnSpan = numberOfColumns;
myItem.Cells.Add(myCell);
}
else
{
for(int indexCount=1;indexCount<=numberOfColumns;indexCount++)
{
TableCell myCell = new TableCell();
myCell.Height = _RowHeight;
myItem.Cells.Add(myCell);
}
}
if(this.PaddingItemStyle.CssClass!=null)
{
myItem.ApplyStyle(this.PaddingItemStyle);
}
return myItem;
}
}
public class BoundColumn : System.Web.UI.WebControls.BoundColumn
{
}
public class ButtonColumn : System.Web.UI.WebControls.ButtonColumn
{
}
public class EditCommandColumn : System.Web.UI.WebControls.EditCommandColumn
{
}
public class HyperLinkColumn : System.Web.UI.WebControls.HyperLinkColumn
{
}
public class TemplateColumn : System.Web.UI.WebControls.TemplateColumn
{
}
}
|
|
|
|
|
You need to check out Event Bubbling:
http://www.odetocode.com/Articles/94.aspx
|
|
|
|
|
|
Hi,edboe
I read your tips,but I can't understand it well.
Can you give me more tips why FixedDataGrid is not work?
thanks
|
|
|
|
|
Hi,edboe
I read your tips,but I can't understand it well.
Can you give me more tips why FixedDataGrid's DeleteCommand,EditCommand,InsertCommand,UpdateCommand and CancelCommand is not work?
thanks
|
|
|
|
|
Hi,
DropDownLists are fine when it comes to databinding or ListItems. BUT what about Both together?
For instance, DropDownList populated with all Emplyees. You will be able to select a Particular employee. And also, I would like to select an option which is "All" which is not in the Database. I have tried a few things, but cant work it out at the moment.
Thanks for any help.
|
|
|
|
|
Just add this after the databinding code:
ddlList.Items.Insert(0, new ListItem("text", "value"));
|
|
|
|
|
You star!
Thank you very much =D. Didn't know that I could add the items from the back end code. I was trying to add it from the Front end.
Much Appreciated.
|
|
|
|
|
No problem. Just don't try and be too clever and do something like this:
ListItem blank = new ListItem("none", "");
ddl1.items.insert(0, blank);
ddl2.items.insert(0, blank);
'cause it doesn't work too well...
|
|
|
|
|
lol,
no no.. but I can do this no? :
ListItem A = new ListItem("All", "");
ListItem B = new ListItem("All", "");
ddl1.items.insert(0, A);
ddl2.items.insert(0, B);
Seeing as I have declared the ListItems separately, it should work well, yes?
|
|
|
|
|
Yep. That's not a problem.
|
|
|
|
|
Hi
I develop one flash intro for my website(ASP.NET using C#).I want to load Home.aspx page after finishing the flash intro automatically no need to skip intro options.
Plz Post me.
Thanks in Advance
|
|
|
|
|
getURL("yoururlhere.com")
|
|
|
|
|
It looks like flash request, Whatever in flash the answer is
<br />
on (load) <br />
{ <br />
getURL("page.html", "target");<br />
}<br />
keep Learning and you never will be out of date...
|
|
|
|
|
With all do respect, wouldn't that load the page before it completes?
I would think adding this to a blank keyframe at the end of your movie would work:
getURL(http://www.whatever.com, "_self");
|
|
|
|
|
To drag and drop the items from listbox1 to listbox2 with the mouse clicks.
when the item from the first listbox1 is draged and droped it into the listbox2, the corresponding items should disappear in the first listbox and it should be appeared in the second listbox.
gane
|
|
|
|
|
Hi Guys,
I have an issue that there must be a simple answer too that I am not seeing. I am developing an Intranet application, where the client wants a Yes/No Megbox, for some input, We are able to use VBscript for this, (All Browser are IE and this is a one Local Application.)
In the previous application in 1.1 there was the same msgbox, that simply wrote the answer to a hidden field on the form using
Document.Form1.OPType.value= "0"
We want to replicate this in ASP.net 2.0
and I realise that they have chnaged the DOM because of Master Pages, so I have attempted to write to the new model like this
document.aspnetForm.OPType.value = "0"
but I am getting an Script error?
Does anybody know how to write hidden field using Vbscript in ASP.net 2.0.
P.S
I am well aware of Vbscript and it's limitations in browsers and Internet Blah Blah Blah , so please avoid answering the question with that advice, as I have a specific reason for doing it this way which I do not want to go into.
"a fool will not learn from a wise man, but a wise man will learn from a fool"
"It is hard to fail, but it is worse never to have tried to succeed." - Theodore Roosevelt
"Success is going from failure to failure without losing your enthusiasm." - Winston Churchill
My Website || My Blog
|
|
|
|
|
cykophysh39 wrote: I realise that they have chnaged the DOM because of Master Pages
No, the DOM has not changed. If you put a control in a container (like a Content control), it's id changes. You can use the ClientID property of the control to find out the id that you can use in client script to access it.
---
b { font-weight: normal; }
|
|
|
|