I'm using a CheckBoxList to define which columns are displayed in a GridView. I populate the CheckBoxList using a query like
select column_name, data_type from information_schema.columns
where table_name = 'myTable'
After the user has chosen the columns (with various Data Types in them) they wish to display, they press a button and the following snippet of VB code generates the GridView.
For Each item As ListItem In chooseColsList.Items
If item.Selected Then
Dim bf As New BoundField()
'PRODUCES BUGGY RESULTS BECAUSE APPLIED TO ALL BoundFields
bf.DataFormatString = "{0:dd-MMM-yyyy}"
bf.ApplyFormatInEditMode = True
bf.DataField = item.Value
bf.HeaderText = item.Value
bf.SortExpression = item.Value
statusReportGrid.Columns.Add(bf)
End If
Next
What I want to do is to apply the DataFormatString ONLY to the columns with a 'date' Data Type in them. However, I have found no way of programmatically determining the Type of the data in the column being bound, no any way of passing this info to the Control. This information exists in the information_schema, as shown in my query, but I don't know how to extract that out and use it to dynamically setup my BoundFields. It is important to note that I'm using an SqlDataSource.
I've experimented with just about every possible solution I can think of and end up here.
Thanks in advance for your help, it is VERY appreciated :)
If you set your check box list like this:
<asp:checkboxlist id="list" runat="server"
DataTextField="column_name" DataValueField="data_type" DataSourceID="YourDSID" />
You should be able to iterate through the items and check the value of the current Item like so:
For Each item As ListItem In chooseColsList.Items
If item.Selected Then
Dim bf As New BoundField()
If StrComp(item.Value,"date") = 0 Then 'Or however datetime is returned
bf.DataFormatString = "{0:dd-MMM-yyyy}"
bf.ApplyFormatInEditMode = True
End If
bf.DataField = item.Text
bf.HeaderText = item.Text
bf.SortExpression = item.Text
statusReportGrid.Columns.Add(bf)
End If
Next
Related
I have this condition:
If IsNothing(ds.Tables(0).Rows(rowData).Item("DATE")) Then
Else
txtDATE.Text = ds.Tables(0).Rows(rowData).Item("DATE")
End If
What ever I do it is the same error.
How can I resolve this?
I have a loop that is going until 10 but my data set can sometimes has
less than 10 rows. I want to create TextBox control dinamically and to
set it text from data set but if for 10 row data set doesn't have data
just to leave text of the TextBox empty
Then use an If to check that condition:
For rowData As Int32 = 0 To 10
' .....
If ds.Tables(0).Rows.Count > rowData Then
Dim dt As Date? = ds.Tables(0).Rows(rowData).Field(Of Date?)("DATE")
txtDATE.Text = If(dt.HasValue, dt.Value.ToString(), "")
Else
txtDATE.Text = ""
End If
Next
I think the column is a nullable date-column(because you're using IsNothing). Then i'd prefer using a Date?, therefore you can use DataRowExtensions.Field which supports nullable types.
Instead of 10 textboxes you can use only one DataGridView control.
Add one column which represent your Date value (this can be done in designer too)
Dim column As New DataGridViewTextBoxColumn With
{
Header = "Date"
DataPropertyName = "DATE"
}
yourDataGridView.Columns.Add(column)
yourDataGridView.AutoGenerateColumns = False
Then use your DataTable as DataSource
yourDataGridView.DataSource = ds.Tables(0)
DataGridView will generate rows only for existed data in DataTable.
You can customize "outfit" of DataGridView your needs.
I have an asp.net listbox on my page, that I want to sort.
E.g: listbox(lstChooseFields)
MY VB code:
Private Sub LoadColumns()
If drpScreen.SelectedIndex > -1 Then
Dim daLookup As New LookupTableAdapters.LookupTableAdapter
Dim dtLookup As New System.Data.DataTable
dtLookup = daLookup.GetNestedControlValues("EM", "ExcelExport.aspx", "drpScreen", "drpScreen", drpScreen.SelectedValue)
lstChooseFields.DataSource = dtLookup
lstChooseFields.DataValueField = "LKP_ControlValue"
lstChooseFields.DataTextField = "LKP_ControlText"
lstChooseFields.DataBind()
'Dispose
daLookup.Dispose()
End If
End Sub
I have done some searching and found something like 'dtLookup.DefaultView.Sort = "LKP_ControlText" that I can use but it does not work. I don't want to sort in my database only on this page, this listbox (lstChooseFields).
So if you want to sort for the datatable then you have tried dtLookup.DefaultView.Sort = "LKP_ControlText" but you have missed out one piece of word there dtLookup.DefaultView.Sort = "LKP_ControlText asc". This generally works if LKP_ControlText is a column name in the datatable
You must use the word ascthere. which i couldnt find in your question.
After getting the dataTable sorted you can copy the same structure to another datatable if u want by using
Dim dtDuplicateLookup As New DataTable()
dtDuplicateLookup = dtLookup.DefaultView.ToTable(True)
and then access dtDuplicateLookup for further use if you want to retain the previous datatable as it is.
Use the .sorted property for the list box as shown here.
lstChooseFields.Sorted = True
UPDATE: your method was right but just tune it this way. I think you missed the DESC part. The second expression tells it how to sort the dataview.
Dim dataView As New DataView(table)
dataView.Sort = " column_name DESC" //DESC or ASC as per requirement
Dim dataTable AS DataTable = dataView.ToTable()
Else try this. After sorting a defaultview, generally you have to loopback through it
foreach(DataRowView r in table.DefaultView)
{
//... here you get the rows in sorted order
//insert the listbox items one by one here using listbox.add or listbox.insert commands
}
I have an asp:CheckboxList on my page and I need to get the values of the checked checkboxes.
So I have a linq query that I then loop through, but I've discovered that there is no way to retrieve the values associated with the individual checkboxes.
Here is my code:
Dim checkboxValues = cblmyCheckboxes.Controls.OfType(Of CheckBox)().Where(Function(c) c.Checked)
For Each c As CheckBox In checkboxValues
Response.Write(c.Value)
Next
How can I get the values?
Thanks
You need to use Selected instead of Checked
var selectedValues = (CheckBoxList1.Items.Cast<ListItem>()
.Where(i => i.Selected)
.Select(i => i.Value))
.ToList();
Dim selectedValues = (CheckBoxList1.Items.Cast(Of ListItem)()
.Where(Function(i) i.Selected)
.[Select](Function(i) i.Value))
.ToList()
Your Checkbox List contains ListItem and not checkbox. So cblmyCheckboxes.Items is a ListItemCollection
Really, a quicker and easier way would be:
For Each li as ListItem in cblmyCheckboxes.Items
If (li.Selected) Then
Dim XX = li.Value
'' Do something with Value
End If
Next
By using LINQ you're effectively looping through the checkbox lists items (in the background of your LINQ function (which is wrong anyway) and then also looping again in your For Each - might as well just do the one...
I have a DropDownList called (DDL) in ASP.net page, I want that DDL contains some records of a table in the data base.
So I did this :
DDL.DataSource = myDataReader
DDL.DataBind()
But it's giving me (5 records) "the number of records of the table" but like this :
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
You should set DataTextField and DataValueField, otherwise data binding will perform .ToString() on every row and put it as item:
DDL.DataSource = myDataReader;
DDL.DataTextField = "[Text column name]";
DDL.DataValueField = "[Value column name]";
DDL.DataBind();
you have to set the text and the key fields of the ddl before you databind
DDL.DataTextField = "textColumn";
DDL.DataValueField = "textColumn":
The code : ddl.datasource=reader is just setting the content present in reader (array of columns of table) as the main source of data.Now as ddl show only a single column in it so u need to write a piece of code which tells ddl that which column it has to display.So you will write: ddl.textfield="column name which you want to show";
and ddl.valuefield="column name which you want as a reference to pass to database";
I am using a codebehind page in ASP.NET to perform a SQL query. The query is loaded into a string, the connection is established (To Oracle), and we get it started by having the connection perform .ExecuteReader into a OleDBDataReader (We'll call it DataRead). I'll try to hammer out an example below. (Consider Drop as an ASP DropDownList control)
Dim LookFor as String = "Fuzzy Bunnies"
While DataRead.Read
If LookFor = DataRead.Item("Kinds of Bunnies") Then
'Meets special critera, do secondary function'
Drop.Items.Add(DataRead.Item("Subgroup of Bunnies"))
...
End if
...
End While
This is the only way I know of doing a dynamic add to a DropDownList. However, each item in a DropDownList has a .text property and a .value property. How can we define the .value as being different from the .text in code?
The Add function can take a ListItem, so you can do
Dim li as new ListItem(DataRead.Item("Subgroup of Bunnies"), "myValue")
Drop.Items.Add(li)
Add should have an overload that accepts a ListItem object. Using that, you can usually do something like this:
Drop.Items.Add(New ListItem("Text", "Value"))
If I understand the question, Items.Add has an overload that takes a ListItem, so you could create a new ListItem object in that line:
Drop.Items.Add(new ListItem("text", "value"))
Pardon my possibly faulty VB
Dim item as New ListItem()
item.Value = "foo"
item.Text = "bar"
Drop.Items.Add(item)
You can also use the ListItem constructor (e.g. new ListItem("text", "value"))
you'd select a second column into your datareader (such as an IDENTITY field) and then assign do your Item generation like this:
Dim item as new listitem
item.text = DataRead.Item("SubGroup Of Bunnies")
item.value = DataRead.Item("ID")
Drop.Items.Add(item)
You may also want to look into the DATABIND functionality, and filtering out "FUZZY BUNNIES" in the SQL statement itself.