kdeutsch
asked on
add query items in datagrid columns
I have a sql query that has gotten out of hand, see file for query. Instead of adding the fields in the query I have them and think I should add them in the datagrid to speed up query, becuase they are sub-select fields and then to do the math I ahve to sub-select them agian which makes the query take longer. How can I add this in my datagrid instead of here. Teh field I would like to eliminate in query is the VAC column in query and then just add teh corresponding columns to get value in datagrid.
help.txt
help.txt
ASKER
Hi,
I already have my fields boung, what I need to do is create a dynamic column that adds the rest of the columns together. Here is my DG, what I need to do is take the AUTH_STR column + ACN column - ASGN_STR to get a new column called VAC
<Columns>
<asp:BoundColumn DataField="UIC" HeaderText="Uic"></asp:Bou ndColumn>
<asp:BoundColumn DataField="City" HeaderText="City"></asp:Bo undColumn>
<asp:BoundColumn DataField="Para" HeaderText="Para"></asp:Bo undColumn>
<asp:BoundColumn DataField="Line" HeaderText="Line"></asp:Bo undColumn>
<asp:BoundColumn DataField="Grade" HeaderText="Grade"></asp:B oundColumn >
<asp:BoundColumn DataField="DMOS" HeaderText="DMOS"></asp:Bo undColumn>
<asp:BoundColumn DataField="Gender" HeaderText="Gender"></asp: BoundColum n>
<asp:BoundColumn DataField="AUTH_STR" HeaderText="Auth_Str"></as p:BoundCol umn>
<asp:BoundColumn DataField="OS" HeaderText="Over_Str"></as p:BoundCol umn>
<asp:BoundColumn DataField="ASGN_STR" HeaderText="Asgn_Str"></as p:BoundCol umn>
<asp:BoundColumn DataField="ACN" HeaderText="ACN_ASGN"></as p:BoundCol umn>
<asp:BoundColumn DataField="Unit_Type" HeaderText="Unit Type"></asp:BoundColumn>
<asp:BoundColumn DataField="Unit_Stat" HeaderText="Unit Stat"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Request">
<ItemTemplate>
<asp:Button ID="btnRequest" runat="server" Text="Request" ForeColor="blue" Width="60" CommandName="Request"></as p:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
I already have my fields boung, what I need to do is create a dynamic column that adds the rest of the columns together. Here is my DG, what I need to do is take the AUTH_STR column + ACN column - ASGN_STR to get a new column called VAC
<Columns>
<asp:BoundColumn DataField="UIC" HeaderText="Uic"></asp:Bou
<asp:BoundColumn DataField="City" HeaderText="City"></asp:Bo
<asp:BoundColumn DataField="Para" HeaderText="Para"></asp:Bo
<asp:BoundColumn DataField="Line" HeaderText="Line"></asp:Bo
<asp:BoundColumn DataField="Grade" HeaderText="Grade"></asp:B
<asp:BoundColumn DataField="DMOS" HeaderText="DMOS"></asp:Bo
<asp:BoundColumn DataField="Gender" HeaderText="Gender"></asp:
<asp:BoundColumn DataField="AUTH_STR" HeaderText="Auth_Str"></as
<asp:BoundColumn DataField="OS" HeaderText="Over_Str"></as
<asp:BoundColumn DataField="ASGN_STR" HeaderText="Asgn_Str"></as
<asp:BoundColumn DataField="ACN" HeaderText="ACN_ASGN"></as
<asp:BoundColumn DataField="Unit_Type" HeaderText="Unit Type"></asp:BoundColumn>
<asp:BoundColumn DataField="Unit_Stat" HeaderText="Unit Stat"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Request">
<ItemTemplate>
<asp:Button ID="btnRequest" runat="server" Text="Request" ForeColor="blue" Width="60" CommandName="Request"></as
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
Are you trying to add/subtract values from the columns or combine them?
If you are trying to combine
Your going to have to fill a DataTable first and then bind to the DG
Here is an example.
If you are trying to combine
Your going to have to fill a DataTable first and then bind to the DG
Here is an example.
dt.Columns.Add(new DataColumn("VAC",
System.Type.GetType("System.String"), "[AUTH_STR] [ACN]"));
ASKER
Hi, here is what i tried but I get a blue underline under New datacolumn to the end
value of type system.data.datacolumn cannot be converted to system.web.ui.webcontrols. datacolumn
SQl = "Select * from tables"
myDataTable = New DataTable
myDataTable = getData(sql)
myDataGrid.Columns.Add(New DataColumn("VAC2", System.Type.GetType("syste m.string") , "[AUTH_STR] + [ACN] - [ASGN_STR]"))
myDataGrid.DataSource = myDataTable
myDataGrid.DataBind()
value of type system.data.datacolumn cannot be converted to system.web.ui.webcontrols.
SQl = "Select * from tables"
myDataTable = New DataTable
myDataTable = getData(sql)
myDataGrid.Columns.Add(New
myDataGrid.DataSource = myDataTable
myDataGrid.DataBind()
myDataGrid.Columns.Add should be myDataTable.Columns.Add
ASKER
Ok changed, but i get the following error. which point tot he new line. I tried putting it after databind and in middle and before, then i tired this. but still got the error below.
myDataTable.Columns.Add(Ne w DataColumn("VAC2", System.Type.GetType("syste m.string") , "[myDataTable.Rows(0)(7)] + [myDataTable.Rows(0)(8)] - [myDataTable.Rows(0)(9)]") )
Error
'dataType' argument cannot be null. Parameter name: dataType
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullExcepti on: 'dataType' argument cannot be null. Parameter name: dataType
myDataTable.Columns.Add(Ne
Error
'dataType' argument cannot be null. Parameter name: dataType
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullExcepti
Are the values in the rows strings or integers?
ASKER
They should be all integers, because I can add them in my sql code. but then it takes to long.
try changing ("system.string") to ("system.integer")
ASKER
Hi,
get he same error as before no matter if its.
myDataTable = New DataTable
myDataTable = getData(sql)
myDataTable.Columns.Add(Ne w DataColumn("VAC2", System.Type.GetType("syste m.integer" ), "[myDataTable.Rows(0)(7)] + [myDataTable.Rows(0)(8)] - [myDataTable.Rows(0)(9)]") )
myDataGrid.DataSource = myDataTable
myDataGrid.DataBind()
or
myDataTable = New DataTable
myDataTable = getData(sql)
myDataTable.Columns.Add(Ne w DataColumn("VAC2", System.Type.GetType("syste m.integer" ), "[AUTH_STR] + [ACN] - [ASGN_STR]"))
myDataGrid.DataSource = myDataTable
myDataGrid.DataBind()
get he same error as before no matter if its.
myDataTable = New DataTable
myDataTable = getData(sql)
myDataTable.Columns.Add(Ne
myDataGrid.DataSource = myDataTable
myDataGrid.DataBind()
or
myDataTable = New DataTable
myDataTable = getData(sql)
myDataTable.Columns.Add(Ne
myDataGrid.DataSource = myDataTable
myDataGrid.DataBind()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
HI,
I tired this from anthoer web site but get a totally different error.
myDataTable.Columns.Add("V AR2", myDataTable.Rows(0)(7))
Here is how they have for DataGridView instead of asp.net 1.1 datagrid
'---adding columns---
DataGridView1.Columns.Add( "ID", "Product ID")
DataGridView1.Columns.Add( "Name", "Product Name")
But the error I get is the follwoing
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastExceptio n: Specified cast is not valid.
I tired this from anthoer web site but get a totally different error.
myDataTable.Columns.Add("V
Here is how they have for DataGridView instead of asp.net 1.1 datagrid
'---adding columns---
DataGridView1.Columns.Add(
DataGridView1.Columns.Add(
But the error I get is the follwoing
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastExceptio
ASKER
Closing out old questions, works
http://authors.aspalliance.com/aspxtreme/webforms/controls/addingboundfieldstogridview.aspx
http://aspnet101.com/aspnet101/tutorials.aspx?id=56
However I would personally use a DataTable first , perform all the processing and then bind to a datagrid