473,511 Members | 16,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Order Columns in bound Datagrid

29 New Member
Hi, I have a bound datagrid in c# using the code below. My problem is that I need to add an extra column as the last column presented which creates a hyperlinkfield using the value of ExpedioRef attached to a URL.

I have tried t do this in the design view of the datagrid using the wizard to add the new column 'History' and field 'view' with value of expedioref but it places the column at the beginning every time. Please could you tell me how to get this column to appear on the left of the other columns, not before.

Alternatively if would be better to use the already ato generated ExpedioRef field i'm returning and add a hyperlink to it, but don't know how to do this.

I can't use the normal datagrid because I am passing in a variable as parameter in the SQL string.

Thanks
Debs



OracleConnection con = new OracleConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["NewExpedioDevString"].ConnectionString;
string cmdQuery = "SELECT R_EXPEDIOREFERENCE ExpedioRef,R_CUSTOMERREFERENCE CustomerRef,R_REQUESTSTATUS Status, R_REQUESTTASK Task FROM aradmin.exp__request WHERE (r_source = 'KANA' AND BFG_CONTRACTID='" + Session["BFGCONTRACTID"].ToString() + "')";

OracleCommand cmd = new OracleCommand(cmdQuery);
try
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();
dtgOpenOrders.DataSource = reader;
dtgOpenOrders.DataBind();
Oct 4 '07 #1
20 1769
Plater
7,872 Recognized Expert Expert
In your SQL statement, have the SELECT return an extra column. That extra column will be your scratch space column and you can then play with/overwrite the data in it.

PS: Unless you're using .NET older then 2.0, you should be using the DataGridView object.
Oct 4 '07 #2
saynords
29 New Member
In your SQL statement, have the SELECT return an extra column. That extra column will be your scratch space column and you can then play with/overwrite the data in it.

PS: Unless you're using .NET older then 2.0, you should be using the DataGridView object.

Hi thanks for such a quick response, yes sorry I am using the gridview object. Could you give me an example of how to do the above (sorry to be a pain - new to all this)
thanks
Debs
Oct 4 '07 #3
Plater
7,872 Recognized Expert Expert
You have an SQL statement that selects say X number of columns (in your case it appears to be 4), just request one of the columns a 2nd time.
I named it SpareColumn below, but you could name it something more suitable to your needs.
Expand|Select|Wrap|Line Numbers
  1. OracleConnection con = new OracleConnection();
  2. con.ConnectionString = ConfigurationManager.ConnectionStrings["NewExpedioDevString"].ConnectionString;
  3. string cmdQuery = "SELECT 
  4. R_EXPEDIOREFERENCE ExpedioRef,
  5. R_CUSTOMERREFERENCE CustomerRef,
  6. R_REQUESTSTATUS Status, 
  7. R_REQUESTTASK Task,
  8. R_EXPEDIOREFERENCE SpareColumn,
  9. FROM aradmin.exp__request WHERE (r_source = 'KANA' AND BFG_CONTRACTID='" + Session["BFGCONTRACTID"].ToString() + "')";
  10.  
  11. OracleCommand cmd = new OracleCommand(cmdQuery);
  12. try
  13. {
  14. con.Open();
  15. cmd.Connection = con;
  16. cmd.CommandType = CommandType.Text;
  17.  
Now you will have an extra column to use as scratch space.
I recomend you transfer into a DataSet (or DataTable) instead of using a datareader.
That way you can manipulate your DataSet before setting it as the datasource for your gridview object.

Alternatively, you can edit the gridview object after the datasource has been applied. You will have that extra column in there to toy with. (I use this in my own project to a pretty good success)
Oct 4 '07 #4
saynords
29 New Member
You have an SQL statement that selects say X number of columns (in your case it appears to be 4), just request one of the columns a 2nd time.
I named it SpareColumn below, but you could name it something more suitable to your needs.
Expand|Select|Wrap|Line Numbers
  1. OracleConnection con = new OracleConnection();
  2. con.ConnectionString = ConfigurationManager.ConnectionStrings["NewExpedioDevString"].ConnectionString;
  3. string cmdQuery = "SELECT 
  4. R_EXPEDIOREFERENCE ExpedioRef,
  5. R_CUSTOMERREFERENCE CustomerRef,
  6. R_REQUESTSTATUS Status, 
  7. R_REQUESTTASK Task,
  8. R_EXPEDIOREFERENCE SpareColumn,
  9. FROM aradmin.exp__request WHERE (r_source = 'KANA' AND BFG_CONTRACTID='" + Session["BFGCONTRACTID"].ToString() + "')";
  10.  
  11. OracleCommand cmd = new OracleCommand(cmdQuery);
  12. try
  13. {
  14. con.Open();
  15. cmd.Connection = con;
  16. cmd.CommandType = CommandType.Text;
  17.  
Now you will have an extra column to use as scratch space.
I recomend you transfer into a DataSet (or DataTable) instead of using a datareader.
That way you can manipulate your DataSet before setting it as the datasource for your gridview object.

Alternatively, you can edit the gridview object after the datasource has been applied. You will have that extra column in there to toy with. (I use this in my own project to a pretty good success)


OK sounds like the second option for me,
the following is my gridview object & binding, could you give me an example of how to edit this object to add a hyerlink to the new SpareColumn fields please, sorry to ask for more but really am stuck on this one. Got some training booked for next week so will leave you alone then ;O)

dtgOpenOrders.DataSource = reader;
dtgOpenOrders.DataBind();
Oct 4 '07 #5
Plater
7,872 Recognized Expert Expert
Well I am sure there is a cleaner way to do this, try something like this:
Expand|Select|Wrap|Line Numbers
  1. protected void procAddingHyperLink()
  2. {
  3.     string msg = "";
  4.     string href = "ManagerSiteInfo.aspx?siteid=";
  5.     int retdateidx = -1;
  6.     if (dtgOpenOrders.HeaderRow != null)
  7.     {
  8.         for (int i = 0; i < gvOverdueSites.HeaderRow.Cells.Count; i++)
  9.         {
  10.             if (dtgOpenOrders.HeaderRow.Cells[i].Text == "Spare Column")
  11.             {//find the column index with that name
  12.                 retdateidx = i;
  13.             }
  14.         }
  15.         if (retdateidx!=-1)
  16.         {
  17.           for (int i = 0; i < gvOverdueSites.Rows.Count; i++)
  18.           {
  19.                 //this would pull the text from the first column
  20.                 msg = dtgOpenOrders.Rows[i].Cells[0].Text;
  21.                 //create a hyperlink object
  22.                 HyperLink hl = new HyperLink();
  23.                 hl.Text = msg;
  24.                 hl.NavigateUrl = href + msg.Replace("&amp;", "&");
  25.                 dtgOpenOrders.Rows[i].Cells[retdateidx].Controls.Add(hl);
  26.           }//end of forloop
  27.       }
  28.     }
  29. }
  30.  
Oct 4 '07 #6
saynords
29 New Member
Well I am sure there is a cleaner way to do this, try something like this:
Expand|Select|Wrap|Line Numbers
  1. protected void procAddingHyperLink()
  2. {
  3.     string msg = "";
  4.     string href = "ManagerSiteInfo.aspx?siteid=";
  5.     int retdateidx = -1;
  6.     if (dtgOpenOrders.HeaderRow != null)
  7.     {
  8.         for (int i = 0; i < gvOverdueSites.HeaderRow.Cells.Count; i++)
  9.         {
  10.             if (dtgOpenOrders.HeaderRow.Cells[i].Text == "Spare Column")
  11.             {//find the column index with that name
  12.                 retdateidx = i;
  13.             }
  14.         }
  15.         if (retdateidx!=-1)
  16.         {
  17.           for (int i = 0; i < gvOverdueSites.Rows.Count; i++)
  18.           {
  19.                 //this would pull the text from the first column
  20.                 msg = dtgOpenOrders.Rows[i].Cells[0].Text;
  21.                 //create a hyperlink object
  22.                 HyperLink hl = new HyperLink();
  23.                 hl.Text = msg;
  24.                 hl.NavigateUrl = href + msg.Replace("&amp;", "&");
  25.                 dtgOpenOrders.Rows[i].Cells[retdateidx].Controls.Add(hl);
  26.           }//end of forloop
  27.       }
  28.     }
  29. }
  30.  
You are an absolute superstar thank you so much, works a treat - think I might have to marry you.
;O)
Oct 4 '07 #7
saynords
29 New Member
Next question - how do i add pageing & Sorting to this? (it's nto available int he design view wizard mode of gridview.

Thanks
Debs
Oct 8 '07 #8
Plater
7,872 Recognized Expert Expert
There is a property in the properies window where you turn this on.
You will need to assign an event for it (it will help you with it)
Oct 8 '07 #9
saynords
29 New Member
There is a property in the properies window where you turn this on.
You will need to assign an event for it (it will help you with it)
I get a system error when i use properties to add paging:

dataSystem.NotSupportedException: The data source does not support server-side data paging. at System.Web.UI.DataSourceView.RaiseUnsupportedCapab ilityError(DataSourceCapabilities capability) at System.Web.UI.DataSourceSelectArguments.RaiseUnsup portedCapabilitiesError(DataSourceView view) at System.Web.UI.WebControls.ReadOnlyDataSourceView.E xecuteSelect(DataSourceSelectArguments arguments) at System.Web.UI.DataSourceView.Select(DataSourceSele ctArguments arguments, DataSourceViewSelectCallback callback) at System.Web.UI.WebControls.GridView.CreateChildCont rols(IEnumerable dataSource, Boolean dataBinding) at System.Web.UI.WebControls.CompositeDataBoundContro l.PerformDataBinding(IEnumerable data) at



Any ideas ?
Thanks
Debs
Oct 8 '07 #10
Plater
7,872 Recognized Expert Expert
Strange, what is your datasource object?
I use paging on my DataTable (that I have edited with extra columns just like we discussed) just fine?
Oct 8 '07 #11
saynords
29 New Member
This is the code I'm using:

OracleConnection con = new OracleConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["NewExpedioDevString"].ConnectionString;
string cmdQuery = "SELECT R_EXPEDIOREFERENCE ExpedioRef,R_CUSTOMERREFERENCE CustomerRef,R_REQUESTSTATUS Status, R_REQUESTTASK Task, R_EXPEDIOREFERENCE History FROM aradmin.exp__request WHERE (r_source = 'KANA' AND BFG_CONTRACTID='" + Session["BFGCONTRACTID"].ToString() + "')";

OracleCommand cmd = new OracleCommand(cmdQuery);
try
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();
dtgOpenOrders.DataSource = reader;
dtgOpenOrders.DataBind();
dtgOpenOrders.AllowPaging = true;
procAddingHyperLink();
procAddingHyperLink2();
}

catch (Exception ex)
{
Response.Write("There was a problem retrieving your data");
Response.Write(ex);
}

finally
{
cmd.Dispose();
con.Close();
con.Dispose();
Oct 8 '07 #12
Plater
7,872 Recognized Expert Expert
Ah ha! You're using DataReader as your datasource.
There is no "seek" ability in datareaders. They can effecitvly only be read through "once".
Use a DataSet or DataTable instead.
Oct 8 '07 #13
saynords
29 New Member
Ah ha! You're using DataReader as your datasource.
There is no "seek" ability in datareaders. They can effecitvly only be read through "once".
Use a DataSet or DataTable instead.
I don;t seem to have that option in the toolbox, only DataList ? I'm using C# is that right?
Thanks so much for your help
Oct 8 '07 #14
saynords
29 New Member
I don;t seem to have that option in the toolbox, only DataList ? I'm using C# is that right?
Thanks so much for your help
oops ignore the last stooopid comment!
Oct 8 '07 #15
Plater
7,872 Recognized Expert Expert
System.Data.DataSet

The namespace is usually already included, so I didn't think to specify it fully.
Oct 8 '07 #16
saynords
29 New Member
System.Data.DataSet

The namespace is usually already included, so I didn't think to specify it fully.

Sorry to ask this but could you give me an example of how to convert this code to use a dataset as I've only just got my head around readers! I am a sever novice but I learn fast and wont need to ask twice! thank you for any help you could give and sorry to be a nuisance.


OracleConnection con = new OracleConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["NewExpedioDevString"].ConnectionString;
string cmdQuery = "SELECT R_EXPEDIOREFERENCE ExpedioRef,R_CUSTOMERREFERENCE CustomerRef,R_REQUESTSTATUS Status, R_REQUESTTASK Task, R_EXPEDIOREFERENCE History FROM aradmin.exp__request WHERE (r_source = 'KANA' AND BFG_CONTRACTID='" + Session["BFGCONTRACTID"].ToString() + "')";

OracleCommand cmd = new OracleCommand(cmdQuery);
try
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();
dtgOpenOrders.DataSource = reader;
dtgOpenOrders.DataBind();
dtgOpenOrders.AllowPaging = true;
procAddingHyperLink();
}

catch (Exception ex)
{
Response.Write(ex);
}

finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}

}

public void procAddingHyperLink()
{
string msg = "";
string href = "default.aspx?ERID=";
int retdateidx = -1;
if (dtgOpenOrders.HeaderRow != null)
{
for (int i = 0; i < dtgOpenOrders.HeaderRow.Cells.Count; i++)
{
if (dtgOpenOrders.HeaderRow.Cells[i].Text == "EXPEDIOREF")
{//find the column index with that name
retdateidx = i;
}
}
if (retdateidx != -1)
{
for (int i = 0; i < dtgOpenOrders.Rows.Count; i++)
{
//this would pull the text from the first column
msg = dtgOpenOrders.Rows[i].Cells[0].Text;
//create a hyperlink object
HyperLink hl = new HyperLink();
hl.Text = msg;
hl.NavigateUrl = href + msg.Replace("&", "&");
dtgOpenOrders.Rows[i].Cells[retdateidx].Controls.Add(hl);
}//end of forloop
}
}
}
Oct 8 '07 #17
Plater
7,872 Recognized Expert Expert
I don't seem to have the Oracle namespace for those objects, but I would imagine it's something like this:
Expand|Select|Wrap|Line Numbers
  1. OracleConnection con = new OracleConnection();
  2. con.ConnectionString = ConfigurationManager.ConnectionStrings["NewExpedioDevString"].ConnectionString;
  3. string cmdQuery = "SELECT R_EXPEDIOREFERENCE ExpedioRef,R_CUSTOMERREFERENCE CustomerRef,R_REQUESTSTATUS Status, R_REQUESTTASK Task, R_EXPEDIOREFERENCE History FROM aradmin.exp__request WHERE (r_source = 'KANA' AND BFG_CONTRACTID='" + Session["BFGCONTRACTID"].ToString() + "')";
  4.  
  5. //OracleCommand cmd = new OracleCommand(cmdQuery);
  6. try
  7. {
  8. con.Open();
  9. /*
  10. cmd.Connection = con;
  11. cmd.CommandType = CommandType.Text;
  12. OracleDataReader reader = cmd.ExecuteReader();
  13. dtgOpenOrders.DataSource = reader;
  14. */
  15. DataSet retval=new DataSet();
  16. OracleDataAdapter dba = new OracleDataAdapter(cmdQuery, con);
  17. dba.Fill(retval);
  18.  
  19. dtgOpenOrders.DataSource=retval;
  20. dtgOpenOrders.DataBind();
  21. dtgOpenOrders.AllowPaging = true;
  22. procAddingHyperLink();
  23. }
  24.  
  25. catch (Exception ex)
  26. {
  27. Response.Write(ex);
  28. }
  29.  
  30. finally
  31. {
  32. cmd.Dispose();
  33. con.Close();
  34. con.Dispose();
  35. }
  36.  
  37. }
  38.  
Oct 8 '07 #18
saynords
29 New Member
I don't seem to have the Oracle namespace for those objects, but I would imagine it's something like this:
Expand|Select|Wrap|Line Numbers
  1. OracleConnection con = new OracleConnection();
  2. con.ConnectionString = ConfigurationManager.ConnectionStrings["NewExpedioDevString"].ConnectionString;
  3. string cmdQuery = "SELECT R_EXPEDIOREFERENCE ExpedioRef,R_CUSTOMERREFERENCE CustomerRef,R_REQUESTSTATUS Status, R_REQUESTTASK Task, R_EXPEDIOREFERENCE History FROM aradmin.exp__request WHERE (r_source = 'KANA' AND BFG_CONTRACTID='" + Session["BFGCONTRACTID"].ToString() + "')";
  4.  
  5. //OracleCommand cmd = new OracleCommand(cmdQuery);
  6. try
  7. {
  8. con.Open();
  9. /*
  10. cmd.Connection = con;
  11. cmd.CommandType = CommandType.Text;
  12. OracleDataReader reader = cmd.ExecuteReader();
  13. dtgOpenOrders.DataSource = reader;
  14. */
  15. DataSet retval=new DataSet();
  16. OracleDataAdapter dba = new OracleDataAdapter(cmdQuery, con);
  17. dba.Fill(retval);
  18.  
  19. dtgOpenOrders.DataSource=retval;
  20. dtgOpenOrders.DataBind();
  21. dtgOpenOrders.AllowPaging = true;
  22. procAddingHyperLink();
  23. }
  24.  
  25. catch (Exception ex)
  26. {
  27. Response.Write(ex);
  28. }
  29.  
  30. finally
  31. {
  32. cmd.Dispose();
  33. con.Close();
  34. con.Dispose();
  35. }
  36.  
  37. }
  38.  

That's fantastic thank you ! pageing now works, sorting doesn;t but ican fiddle about with that.
The problem now is my method to add a column no longer reads the dtgOpenOrders.HeaderRow.Cells[i].Text . Te project builds but when I response.write this line it is blank where before it would return the column header.

Sorry to pester you, you've been a massive help already but any idea? is it something staring me in the face?!



public void procAddingHyperLink()
{
string msg = "";
string href = "default.aspx?ERID=";
int retdateidx = -1;
if (dtgOpenOrders.HeaderRow != null)
{
for (int i = 0; i < dtgOpenOrders.HeaderRow.Cells.Count; i++)
{

if (dtgOpenOrders.HeaderRow.Cells[i].Text == "EXPEDIOREF")
{//find the column index with that name
retdateidx = i;
}
}
if (retdateidx != -1)
{
for (int i = 0; i < dtgOpenOrders.Rows.Count; i++)
{
//this would pull the text from the first column
msg = dtgOpenOrders.Rows[i].Cells[0].Text;
//create a hyperlink object
HyperLink hl = new HyperLink();
hl.Text = msg;
hl.NavigateUrl = href + msg.Replace("&", "&");
dtgOpenOrders.Rows[i].Cells[retdateidx].Controls.Add(hl);
}//end of forloop
}
}
}
Oct 9 '07 #19
Plater
7,872 Recognized Expert Expert
Are your columns labeled when you load the page?
If they are, there should be something in the HeaderRow.
Have you debuged to see what it claims is in there?
Oct 9 '07 #20
saynords
29 New Member
Are your columns labeled when you load the page?
If they are, there should be something in the HeaderRow.
Have you debuged to see what it claims is in there?
I am creating the 2 columns (in bold) via SQL query and changing the contents of the data in that field of each row via the 2 methods which was working ok and it iterates through each cell but returns a blank string when I response.write the line dtgOpenOrders.HeaderRow.Cells[i].Text which is the line that should bring back the columen headers ;O(

string cmdQuery = "SELECT R_EXPEDIOREFERENCE ExpedioRef,R_CUSTOMERREFERENCE CustomerRef,R_REQUESTSTATUS Status, R_REQUESTTASK Task, R_EXPEDIOREFERENCE History FROM aradmin.exp__request WHERE (r_source = 'KANA' AND BFG_CONTRACTID='" + Session["BFGCONTRACTID"].ToString() + "')";
Oct 9 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

7
7656
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
0
717
by: Paul | last post by:
Hey there, I have a DataGrid bound to a DataTable. When a user selects a row, I want to send the UNQ_ID (which is in a column with a width of zero, or it's visible parameter set to false) for...
9
9581
by: Frederik | last post by:
Hi all, I'm building a C# application that uses a M$ Acces database. In one of the queries I use something like the following: SELECT id FROM mytable WHERE id IN (20, 12, 21, 14) The result...
4
5398
by: | last post by:
I have a datagrid with a template column that has a hyperlink and a label. The hyperlink text is bound to Title from my dataset and the label text is bound to Author in the dataset. The grid...
1
840
by: Amber | last post by:
The DataGrid allows you to make columns visible or invisible on demand - even edit and other special columns. This article will show you how it is done. Some developers have reported problems...
3
1328
by: Allen K | last post by:
Hi, I'm programmatically creating a HyperLink column for my datagrd ( in addition to Bound Columns created through the VS.NET Visual Interface ). However, when a Postback occurs, this column...
9
3167
by: tshad | last post by:
I have a datagrid that I want to add a new column to. This column will only be visible under certain conditions. So I want to set the column visible=false. Then when the right condition happens...
6
2529
by: Opa | last post by:
Hi, I have a DataGrid, whose sourceI am exporting to Excel. This works fine except for the Column ordering. My datasource is not a datatable, with a typical SELECT statement where I can...
1
2103
by: Brett Wesoloski | last post by:
I am new to using template columns. I am just trying to create a data grid with a bound column and another column with a imagebutton in it. What I have always done in the past was to then create...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.