<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Hello,</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y:
hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">My question is: Can I add in new column(S) in the gridview if the column is not found
in the database?</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> - E.g. An indicator if a certain lecturer is a ML/Lec (roles).</div> <div style="position: absolute;
left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">I have
a gridview that is "extracted" out from the database.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">But I want to add in a new row which is not in the database
but links to the data in the database.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">For example, in the database, I know that a certain lecturer is a module
leader, staff.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">But I want to have an extra column which can indicate in itself (probably a tick/mark) that the
following name (prev column) is a module leader or staff; and i am not allowed to add new column in the database.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div>
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Any help appreciated.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x:
hidden; overflow-y: hidden;" id="_mcePaste">Need me to further explain my queries, let me know. :)</div>
Hello,
My question is: Can I add in new column(S) in the gridview if the column is not found in the database?
- E.g. An indicator if a certain lecturer is a ML/Lec (roles).
I have a gridview that is "extracted" out from the database.
But I want to add in a new row which is not in the database but links to the data in the database.
For example, in the database, I know that a certain lecturer is a module leader, staff.
But I want to have an extra column which can indicate in itself (probably a tick/mark) that the following name (prev column) is a module leader or staff; and i am not allowed to add new column in the database.
Any help appreciated.
Need me to further explain my queries, let me know. :)
If yes, you can introduce a column in the sql itself, for example:
"SELECT Col1, Col2, Col3, Col4, Col5, '' As Leader
FROM sometablename
WHERE ........."
and then manipulate Cells[5].Text further with any sign or symbol in the
RowDataBound event as the rows are bound. As you want to compare the previous column you can check the underlying data for that column with the DataItem in the GridViewRowEventArgs e passed in to the RowDataBound event,
DataRowView drv = e.Row.DataItem as DataRowView;
//and then do the check like:
if (drv["LeaderColumnName"] == "ModuleLeader")
{
//set e.Row.Cells[5].Text = "Yes" ;
}
that would be simple enough to do...maybe add color to the cell or other formatting etc
And IF you have AutoGeneratedColumns="false" then you can always add another TemplateField with an ItemTemplate which would allow you any control like a Label or ImageButton etc. Again, you'd use the RowDataBound event except that this time you'll do a FindControl
to get to the templated control and then set its properties. simple example:
http://www.codekeep.net/snippets/61a14e9c-c14f-4611-814e-99146e1f80e2.aspx
p.s. if you use vb.net then you can convert code
here
Nope nope. It's not really auto generated. Erm.. how do i say..
This is from one of the page(some of the codes) where users can filter what they want to search from.
no you don't have to set it to true; just FYI: we do that when we don't know the database columns that are being included in the results OR when the columns themselves are dynamic (sometimes col1 and col2 are fetched while some other time col3 and
col4 are); when it is set to "true" the GridView generates columns accordingly, refer:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns.aspx
Like I mentioned earlier, you can take the second option and, say, use a Label control for the extra column that you want:
and in the code behind the handler: protected void gridAMDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the Label control using FindControl and set its Text on some column value
DataRowView row = (DataRowView)e.Row.DataItem;
if (row["previouscolumnname"] == "ModuleLeader")
{
Label lbl = e.Row.FindControl("Label1") as Label;
if (lbl != null)
{
lbl.Text = "Yes";
}
}
}
}
replace previouscolumnname with your actual 'previous' column name ...as also the actual value you're comparing to etc.
husna_
Member
6 Points
44 Posts
Gridview - Adding Columns
Sep 09, 2009 01:29 AM|LINK
Hello,
My question is: Can I add in new column(S) in the gridview if the column is not found in the database?
- E.g. An indicator if a certain lecturer is a ML/Lec (roles).
I have a gridview that is "extracted" out from the database.
But I want to add in a new row which is not in the database but links to the data in the database.
For example, in the database, I know that a certain lecturer is a module leader, staff.
But I want to have an extra column which can indicate in itself (probably a tick/mark) that the following name (prev column) is a module leader or staff; and i am not allowed to add new column in the database.
Any help appreciated.
Need me to further explain my queries, let me know. :)
C# gridview column
PeteNet
All-Star
81342 Points
11398 Posts
Re: Gridview - Adding Columns
Sep 09, 2009 03:23 AM|LINK
If yes, you can introduce a column in the sql itself, for example:
"SELECT Col1, Col2, Col3, Col4, Col5, '' As Leader
FROM sometablename
WHERE ........."
and then manipulate Cells[5].Text further with any sign or symbol in the RowDataBound event as the rows are bound. As you want to compare the previous column you can check the underlying data for that column with the DataItem in the GridViewRowEventArgs e passed in to the RowDataBound event,
DataRowView drv = e.Row.DataItem as DataRowView;
//and then do the check like:
if (drv["LeaderColumnName"] == "ModuleLeader")
{
//set e.Row.Cells[5].Text = "Yes" ;
}
that would be simple enough to do...maybe add color to the cell or other formatting etc
And IF you have AutoGeneratedColumns="false" then you can always add another TemplateField with an ItemTemplate which would allow you any control like a Label or ImageButton etc. Again, you'd use the RowDataBound event except that this time you'll do a FindControl to get to the templated control and then set its properties. simple example: http://www.codekeep.net/snippets/61a14e9c-c14f-4611-814e-99146e1f80e2.aspx
p.s. if you use vb.net then you can convert code here
Peter
vijayst
All-Star
16558 Points
3216 Posts
Microsoft
Re: Gridview - Adding Columns
Sep 09, 2009 03:59 AM|LINK
Another approach would be to calculate the data in the SQL Query itself.
SELECT a, b, a*b/5 AS c FROM MathTable
Here c is a calculated column.
http://liteblog.codeplex.com
husna_
Member
6 Points
44 Posts
Re: Gridview - Adding Columns
Sep 09, 2009 04:22 AM|LINK
Hello Peter,
Nope nope. It's not really auto generated. Erm.. how do i say..
This is from one of the page(some of the codes) where users can filter what they want to search from.
C# gridview column
husna_
Member
6 Points
44 Posts
Re: Gridview - Adding Columns
Sep 09, 2009 04:31 AM|LINK
Hello Vijayst,
Erm. I am not computing actually.
I just want to show out the column which is derived from another column.
Let's say If blabla is a module leader, it will show out in a new row (called role) as ML.
PeteNet
All-Star
81342 Points
11398 Posts
Re: Gridview - Adding Columns
Sep 09, 2009 04:31 AM|LINK
I was refering to the AutoGeneratedColumns property on the GridView; I'm still not sure whether you have it set or not. for example:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataSourceID="XmlDataSource2">......</asp:GridView>
Peter
husna_
Member
6 Points
44 Posts
Re: Gridview - Adding Columns
Sep 09, 2009 04:40 AM|LINK
Hello Peter,
Nope, I did not set it to true.
<asp:GridView ID="gridAMDisplay" runat="server" BorderColor="Black" BorderStyle="Solid" AutoGenerateColumns="False" style="border-right: #000000 3px solid; border-top: #000000 3px solid; border-left: #000000 3px solid; border-bottom: #000000 3px solid" Width="993px"> <RowStyle BorderColor="White" Font-Bold="True" /><Columns> <asp:BoundField HeaderText="Module" DataField="ModuleCode" > <HeaderStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" /> <ItemStyle BorderColor="Black" BorderStyle="Solid" /> </asp:BoundField> .................. ................. .............. </asp:GridView>Do i have to set it to true?
What is the difference whether i put it as false or true?
Husna
C# gridview column
manoj0682
Star
8479 Points
1499 Posts
Re: Gridview - Adding Columns
Sep 09, 2009 04:52 AM|LINK
when AutoGenerateColumns is true, Bound Field generate automatically. So you don't have to mention Bound Field expictly.
When AutoGenerateColumns is false, Bound Field does not generate. So you have to mention Bound Field.
I think in ur case let AutoGenerateColumns should be False
Manoj Karkera
husna_
Member
6 Points
44 Posts
Re: Gridview - Adding Columns
Sep 09, 2009 04:55 AM|LINK
Hello Manoj Karkera,
If that's the case, setting it to false, am i able to still use peter's suggestion?
PeteNet
All-Star
81342 Points
11398 Posts
Re: Gridview - Adding Columns
Sep 09, 2009 04:59 AM|LINK
no you don't have to set it to true; just FYI: we do that when we don't know the database columns that are being included in the results OR when the columns themselves are dynamic (sometimes col1 and col2 are fetched while some other time col3 and col4 are); when it is set to "true" the GridView generates columns accordingly, refer: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns.aspx
Like I mentioned earlier, you can take the second option and, say, use a Label control for the extra column that you want:
wire up the RowDataBound event:
<asp:GridView ID="gridAMDisplay" runat="server" BorderColor="Black" BorderStyle="Solid" AutoGenerateColumns="False" style="border-right: #000000 3px solid; border-top: #000000 3px solid; border-left: #000000 3px solid; border-bottom: #000000 3px solid" Width="993px" OnRowDataBound="gridAMDisplay_RowDataBound">
add a templatefield to the existing <Columns>, example:
<asp:TemplateField HeaderText="Leader">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="" />
</ItemTemplate>
</asp:TemplateField>
and in the code behind the handler:
protected void gridAMDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the Label control using FindControl and set its Text on some column value
DataRowView row = (DataRowView)e.Row.DataItem;
if (row["previouscolumnname"] == "ModuleLeader")
{
Label lbl = e.Row.FindControl("Label1") as Label;
if (lbl != null)
{
lbl.Text = "Yes";
}
}
}
}
replace previouscolumnname with your actual 'previous' column name ...as also the actual value you're comparing to etc.
Peter