Showing posts with label column. Show all posts
Showing posts with label column. Show all posts

Monday, March 26, 2012

populate a dg column based upon the value populated to another row in the datagrid

Is it possible when populating a datagrid to populate a dropdownlist
column based upon the value populated to another row in the datagrid?
(i.e. I have a drop down which I want to populate differently for each
row with user names based upon a UserRegionID which is also a column in
the row).
Since you don't actually know the value of the UserRegionID until the
data is already bound to the datagrid, how do you do this?
Thanks,
Mike
*** Sent via Developersdex http://www.examnotes.net ***You can use the datagrid events (RowDataBound I think) to handle this
by populating your dropdown using the id of each row as the grid is
populated
HTH
---
David Gray
ASP.NET/C# Developer/Architect (MCAD.NET)
On Fri, 13 Jan 2006 02:23:16 -0800, Mike P <mike.parr@.gmail.com>
wrote:

>Is it possible when populating a datagrid to populate a dropdownlist
>column based upon the value populated to another row in the datagrid?
>(i.e. I have a drop down which I want to populate differently for each
>row with user names based upon a UserRegionID which is also a column in
>the row).
>Since you don't actually know the value of the UserRegionID until the
>data is already bound to the datagrid, how do you do this?
>
>Thanks,
>Mike
>
>*** Sent via Developersdex http://www.examnotes.net ***

populate a dg column based upon the value populated to another row in the datagrid

Is it possible when populating a datagrid to populate a dropdownlist
column based upon the value populated to another row in the datagrid?
(i.e. I have a drop down which I want to populate differently for each
row with user names based upon a UserRegionID which is also a column in
the row).

Since you don't actually know the value of the UserRegionID until the
data is already bound to the datagrid, how do you do this?

Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***You can use the datagrid events (RowDataBound I think) to handle this
by populating your dropdown using the id of each row as the grid is
populated

HTH

-------------
David Gray
ASP.NET/C# Developer/Architect (MCAD.NET)

On Fri, 13 Jan 2006 02:23:16 -0800, Mike P <mike.parr@.gmail.com>
wrote:

>Is it possible when populating a datagrid to populate a dropdownlist
>column based upon the value populated to another row in the datagrid?
>(i.e. I have a drop down which I want to populate differently for each
>row with user names based upon a UserRegionID which is also a column in
>the row).
>Since you don't actually know the value of the UserRegionID until the
>data is already bound to the datagrid, how do you do this?
>
>Thanks,
>Mike
>
>*** Sent via Developersdex http://www.developersdex.com ***

Populate Database from Arraylist

I would like to know how to Populate the SQL Database column using the Arraylist

I'm sure its gonna be INSERT...one example i need

INSERT INTO tempPrice(price) VALUES ( "...............")

How should i declare Arraylist in it? I have a list of values inside the arraylist and i just want the entire column to be filled with it.

Thanks.

HTM

Unfortunately there is no direct way to do this. You have to execute each sql query for each row.

Regards

Saturday, March 24, 2012

Populate dropdownlist using table data + text

I have a drop down list and the first option need to be 'ALL', then the remaining options are retrieved from a column of a table. How can this be achieved?
Thanks...I'm not sure it's the best way of doing it, but this is what I do (I'm using a DataSet, it is only mildly different with a SQLDataReader):
Dim myDS as DataSet
' Fill myDS here
Dim myDR as DataRow
myDropDownList.Items.Add(new ListItem("All"))
For Each myDR in myDS.Tables(0).Rows
myDropDownList.Items.Add(new ListItem(myDR.Items("ColumnNameOrIndexHere")))
Next
I do this because the obvious issue with using DataBind is that it flushes whatever data you already had in there. However, looking at the documentation for ListItems, it appears you can do this:
'Get data and bind it to myDropDownList (assuming you are already doing this)
myDropDownList.Items.Insert(0, new ListItem("All"))
The documentation for ListItemCollection in DropDownList (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionmemberstopic.asp) might help if neither of these are what you are looking for.

// Bind the DDL to the datasource first...
DDL.DataValueField = "value-column-name-here";
DDL.DataTextField = "text-column-name-here";
DDL.DataSource = dataTable;
DDL.DataBind();

// Add the "ALL" selection at the 1st (#0) position...
DDL.Items.Insert(0, new ListItem("ALL", "ALL"));
NC...


I think the most quicker way is to populatethe ArrayList from the Database using the SqlDataReader (In case youare using SQL Server Database).
The first element of the ArrayList will be "All" which you can simply add like this:
ArrayList a = new ArrayList();
a.Add("All");
after that you use SqlDataReader to populate the ArrayList and finally bind the list to the DropDownList.
Let me know if you need to know more about this technique!

And just how could you possibly think that going through the process of creating an ArrayList out of the data and then binding the ArrayList to the DDL be faster and better than just binding the DDL directly to the data?
NC...

It all depends on how many rows are in the Database Table and how many you are inserting in the DataSet.

In what way? Creating the ArrayList would take more time regardless, even not taking into account the GC having to be called to discard it afterwards.
NC...

DataSet is a collection of DataTables, DataRows and DataColumns so does;nt that make DataSet more heavier than an ArrayList.

Why wouldn't you just use the DataReader as the data source?
DDL.DataValueField = "value-column-name-here";
DDL.DataTextField = "text-column-name-here";
DDL.DataSource = dataReader;
DDL.DataBind();

// Add the "ALL" selection at the 1st (#0) position...
DDL.Items.Insert(0, new ListItem("ALL", "ALL"));
dataReader.Close();
Then you don't have the overhead of creating the ArrayList, or a collection of any kind. After all, you'll never need the ArrayList again after binding the DDL.
NC...


Ohh no no no!
Don't ever send the DataReader on the presentation layer. DataReaderworks while the connection is open so this means you are sending theopen connection on the presentation layer.
NEVER NEVER NEVER send the DataReader on the presentation layer. Thisis also like mixing the DataBase layer with presentation layer with noBLL in between.

azamsharp wrote:

Ohh no no no!
Don't ever send the DataReader on the presentation layer. DataReader works while the connection is open so this means you are sending the open connection on the presentation layer.
NEVER NEVER NEVER send the DataReader on the presentation layer. This is also like mixing the DataBase layer with presentation layer with no BLL in between.


Weren't you the one transferring the DataReader into an ArrayList? My data layer does nothing but produce DataTables and DataSets, so there are no Connection objects, Command objects, DataReader objects, etc in my presentation layer (nor in the entire application layer for that matter).
Here is the part that I disagree with you on:

azamsharp wrote:

I think the most quicker way is to populate the ArrayList from the Database using the SqlDataReader (In case you are using SQL Server Database).


It's no quicker to create an ArrayList than a DataTable from a DataReader (if you need the code, let me know and I'll post it). In fact, unless you create a custom class and a custom collection, you'll be very restricted with an ArrayList as you have no access to column names to attach the DataValueField and DataTextField properties of the DDL, so you'll have to use the same value for both.
NC...


>>Weren't you the one transferring the DataReader into an ArrayList?
Thats done in the business logic layer and not the presentation layer which you have demonstrated.


I would agree with azamsharp if it was an application with a static connection. However, in the case of a web page, your web server is maintaining the DB Connection either while you are populating the ArrayList from the DataReader or while performing the DataBind. No connection is maintained by the client computer, only the web server.

Well then we're talking the same thing then. My way uses a DataTable and your way uses an ArrayList. both constructed in the data layer.

The main problem, as I see it, is that using the ArrayList, you'll be restricted to one column (unless you create a custom class, stuffing that into the ArrayList, which will also take more time and code), since you have no access to column names in an ArrayList to attach to the DataValueField and DataTextField properties of the DDL, therefore, you'll have to use the same value for both.

NC...

Wednesday, March 21, 2012

Populating 2 dim array.

Hello, I have a 7 column data table in ADO.net with 200 rows. I want to learn how to copy the data in my data table to myarray (6,199) as Integar.

Dim myarray (6, 199) As Integar??

For Each myarray In myDataSet.Tables??

??

??

Next??

Hello my friend,

Use the following: -

Dim myarray(6, 199) As Integer

For i As Integer = 0 To dt.Rows.Count - 1

For j As Integer = 0 To dt.Columns.Count - 1
myarray(i, j) = Convert.ToInt32(dt.Rows(i).Item(j))
Next
Next

Kind regards

Scotty

Populating a datagrid bound list view by a query

Hi,
My question today is below is my code which binds a listview to a databound column in my datagrid, but at the moment the list items are hard coded in. What I actually want is for the listitems to be populated from a lookup style query so possible from a data reader bringing back the ID which will be stored in the bound column but so the user just sees the Text associated with the value. Hope that makes sense.

<asp:TemplateColumn HeaderText="Model">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FK_StockModelID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropModel" Runat="server">
<asp:ListItem Value="1" text="1"></asp:ListItem>
<asp:ListItem Value="2" text="2"></asp:ListItem>
<asp:ListItem Value="3" text="3"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="DropModel" text="*" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>

I think I can pretty much suss this out if I can work out how to access the dropdown list from tthe codebehind. So if anyone knows that as a starting point, please enlighten me.

PS sorry about the smilies in the code the ASP syntax just happent to be the same code.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp
Ok I found this code at the above link but Im struggling to use it, can anyone explain it line by line or tell me how to make it reference my template item shown above.
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim Cu As String = DRV("ShipVia")
Dim DDL As DropDownList = CType(e.Item.Cells(4).Controls(1), DropDownList)()
Dim SQL As String = _
"SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
Dim DS As New DataSet
Dim item As ListItem
DA.Fill(DS, "Shippers")
DDL.DataSource = DS.Tables("Shippers").DefaultView
DDL.DataTextField = "CompanyName"
DDL.DataValueField = "ShipperID"
DDL.DataBind()
item = DDL.Items.FindByValue(CurrentShip)
If Not item Is Nothing Then item.Selected = True
End If
End Sub
Yes, you'd do it in the ItemDataBound event. After checking for the ItemType, declare a variable of type Combobox. Set it to e.Item.Cells(x).FindControl(your combobox id), and set the value of that combobox to the value you want from the dataset. You can use e.Item.ItemIndex to get the row number in the repeater/grid/dataset.
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim cboModel As DropDownList
Dim cboModel As DropDownList = e.Item.Cells(FindControl(DropModel))
'Also I have put DopModel as the ID of the Dropdown list in the item template but it doesnt seem to be recognised? Name DropModel is not declared

End If
End Sub
Fixed

Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")

Now to try the next bit...
Nearly there, I am now just getting an error at run time on the line underlined saying cannot convert dbnull to string?
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim lstItem As ListItem
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentModel As String = DRV("FK_StockModelID")
Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")
Main.objCarriageBuildSheet.GetStockModels() 'Populates a dataset
cboModel.DataSource = Main.objCarriageBuildSheet.dds_StockModels.dt_StockModels
cboModel.DataTextField = "Model"
cboModel.DataValueField = "FK_StockModelID"
cboModel.DataBind()
lstItem = cboModel.Items.FindByValue(CurrentModel)
If Not lstItem Is Nothing Then lstItem.Selected = True
End If
End Sub
DataBinder.Eval(e.Item.DataItem, "FK_StockModelID")
Like this ?
If e.Item.ItemType = ListItemType.EditItem Then

Dim lstItem As ListItem
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentModel As String = DataBinder.Eval(e.Item.DataItem, "FK_StockModelID") 'DRV("FK_StockModelID")
Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")

Main.objCarriageBuildSheet.GetStockModels() 'Populates a dataset
cboModel.DataSource = Main.objCarriageBuildSheet.dds_StockModels.dt_StockModels
cboModel.DataTextField = "Model"
cboModel.DataValueField = "FK_StockModelID"
cboModel.DataBind()
lstItem = cboModel.Items.FindByValue(CurrentModel)

If Not lstItem Is Nothing Then lstItem.Selected = True

End If

I still get same error
Hmm... maybe before you call that line, you should check whether e.Item.DataItem evaluates to null... place a breakpoint there and have a look at it.
It evaluates to a system.data.datarowview object
Hi Mendhak, I have finally solved the problem a different way.

My HTML code looks like this now, the main difference is I have set the datasource to a function in my code behind page.
<asp:TemplateColumn HeaderText="Model">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FK_StockModelID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=DropModel Runat="server" DataValueField="ModelID" DataTextField="BrandModel" DataSource="<%# PopulateList %>" >
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" Runat="server" text="*" ControlToValidate="DropModel"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateColumn>

The function simply returns a previously filled dataset.
Public Function PopulateList() As DataSet

Return Main.objCarriageBuildSheet.dds_StockModels
End Function

this seems to be working nicely.

Friday, March 16, 2012

populating dropdown in datagrid

I am populating a drop down column in a datagrid on page load. Here is
my code :
<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList ID="ddlUserName" Font-Name="Verdana"
Font-Size="8pt" Runat=server
DataValueField="UserName"
DataTextField="UserName"
DataSource='<%# GetUserList() %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
public DataSet GetUserList()
{
SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strAtradius"]);
string strUserList = "select username from userlogin where jobrolekey
= 2 ";
strUserList += "order by username";
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strUserList,
objConnection);
objDataAdapter.Fill(dsUsers, "Users");
return dsUsers;
}
dsUsers is global to the page. The problem I have is that the first row
of the datagrid populates correctly, but the following lines append the
contents of the dropdown in the row above. How do I clear the contents
of the dropdown before populating each row?
Any help would be really appreciated.
Cheers,
Mike
*** Sent via Developersdex http://www.examnotes.net ***The code that you posted shows that you return an object named dsUsers but i
t
does not show when the object was instantiated. If the scope of that
variable "dsUsers" is outside the scope of the function GetUsersList then
your table would keep appending records for each row of the datagrid
processed. You should move the steps of populating the table to the
Page_Load if they are not dependent on any value in the datagrid item
otherwise you code has to change to return only a DataTable that is local in
scope to the "GetUsersList" function.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Mike P" wrote:

> I am populating a drop down column in a datagrid on page load. Here is
> my code :
> <asp:TemplateColumn>
> <ItemTemplate>
> <asp:DropDownList ID="ddlUserName" Font-Name="Verdana"
> Font-Size="8pt" Runat=server
> DataValueField="UserName"
> DataTextField="UserName"
> DataSource='<%# GetUserList() %>'>
> </asp:DropDownList>
> </ItemTemplate>
> </asp:TemplateColumn>
>
> public DataSet GetUserList()
> {
> SqlConnection objConnection = new
> SqlConnection(ConfigurationSettings.AppSettings["strAtradius"]);
> string strUserList = "select username from userlogin where jobrolekey
> = 2 ";
> strUserList += "order by username";
> SqlDataAdapter objDataAdapter = new SqlDataAdapter(strUserList,
> objConnection);
> objDataAdapter.Fill(dsUsers, "Users");
> return dsUsers;
> }
> dsUsers is global to the page. The problem I have is that the first row
> of the datagrid populates correctly, but the following lines append the
> contents of the dropdown in the row above. How do I clear the contents
> of the dropdown before populating each row?
>
> Any help would be really appreciated.
>
> Cheers,
> Mike
>
>
> *** Sent via Developersdex http://www.examnotes.net ***
>