Import specific columns of a DataTable in Visual Basic - data-binding

I have a DataTable with multiple columns in it, and i would like to import some of the columns with the data of every row of it to a DataGridView in VisualBasic.
Can you please help me?

Your question is very very generic. In either case here is a sample.
'Create a new datatable
Dim table2 As New DataTable
table2.Columns.Add("Name")
'loop through your existing datatable - add the records to the columns you want
For Each dr As DataRow In Table1.Rows
Dim R As DataRow = dt.NewRow
R("Name") = dr("TABLE1_COLUMNNAME")
dt.Rows.Add(R)
Next
'turn the new datatable into the datagridview.
DataGridView1.DataSource = table2

Related

How to use added table in tablecell asp.net

I added the table in TableCell
Dim rSkala As New TableRow()
Dim cSkala As New TableCell()
cSkala.Controls.Add(New Table())
rSkala.Cells.Add(cSkala)
How can I use this table now? I want add to the table rows and cells, but this table haven't name
You must assign programmatically the ID of the control:
Dim rSkala As New TableRow()
Dim cSkala As New TableCell()
Dim newTable as New Table()
newTable.ID = "myID"
cSkala.Controls.Add(newTable)
rSkala.Cells.Add(cSkala)
At this point your table has an ID to be referenced (es. with FindControl("myID"))

How can I access the rows in a certain column of a DataTable?

I need to populate the rows in specific columns of a DataTable. I have the following setup:
Dim Dt As DataTable = New DataTable()
Dim Da As SqlDataAdapter = New SqlDataAdapter()
Dt.Columns.Add("Column 0")
Dt.Columns.Add("Column 1")
Try
SQLConn.ConOpen()
Da.SelectCommand = SQLcmd_locCodes
Da.Fill(Dt)
SQLConn.ConClose()
Catch ex As Exception
SQLConn.ConClose()
End Try
The SqlDataAdapter is filling the table with data from a database. I need to be able to store the data from the database into specific columns in the DataTable. This is what I'm trying (I'm populating a barchart with the data, but you can ignore that part):
For i As Integer = 0 To Dt.Rows().Count - 1
chart.Series("Series1").Points.AddY(Dt.Rows(i)("Column 0").ToString())
chart.Series("Series1").Points(i).AxisLabel = Dt.Rows(i)("Column 0").ToString()
Next
I know that I'm probably doing some simple task incorrectly. Am I going about accessing the particular rows incorrectly? Any help is appreciated.
You can iterate the rows in the DataTable like this:
For Each dr As DataRow in Dt.Rows
chart.Series("Series1").Points.AddY(dr("Column 0").ToString())
chart.Series("Series1").Points(i).AxisLabel = dr("Column 0").ToString())
Next
However, you are better off removing this:
Dt.Columns.Add("Column 0")
Dt.Columns.Add("Column 1")
Just let the DataAdapter name the columns per the column names specified in the query.
Check out my Question & answer (Bottom of page) from Yesterday
Why I am getting System.Data.DataRowView” instead of real values from my Listbox in vb.net
This can be used to extract the information from individual row locations of selected columns.

How to display a single row in asp.net web forms?

I ask myself how I can display a single row from a datatable object in a control like a gridview.
I already did it with label objects like here: (this is in load event. I already have buttons which increment the zero and decrement)
Tbname.Text = (dset.Tables("coduta").Rows(0).Item("Firma"))
TbStraße.Text = (dset.Tables("coduta").Rows(0).Item("Straße_Firma"))
TbHausnummer.Text = (dset.Tables("coduta").Rows(0).Item("Hausnummer_Firma"))
TbOrt.Text = (dset.Tables("coduta").Rows(0).Item("Ort_Firma"))
the point is I want to show the specific row in something like a gridview control. The only Idea i have is, to create a new table out of the row and that looks like a too complicated way for this. I hope guys can help
cheers steven
I am from C# background but this approach should help you.
Get first row from existing table.
Make clone of existing table.
Add that row to clone table.
Assign that table as datasource for grid
DataRow dr = dset.Tables("coduta").Rows(0);
DataTable dtNew = dset.Tables("coduta").Clone();
dtNew.Rows.Add(dr.ItemArray);
grid.DataSource = dtNew;
grid.DataBind();
try
da = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.SelectCommand = new SqlCommand(#"SELECT * FROM coduta", connString);
da.Fill(ds, "coduta");
dt = ds.Tables["coduta"];
foreach (DataRow dr in dt.Rows)
{
//here is your row of data
}

how to position column names vertically in datagridview in vb.net

I appologise in advance if my question may appear silly to you but I have a problem with positioning column names in a dataGridview vertically. I'm populating DataGridView using flowing code:
Dim StrQwery As String = "SELECT * FROM employees WHERE employee_id = (select MAX(employee_id) FROM employees AND registered = 'UNREGISTERED';
Dim smd As MySqlCommand
smd = New MySqlCommand(StrQwery, myconn)
smd.CommandType = CommandType.Text
Dim da As New MySqlDataAdapter(smd)
Dim cb As New MySqlCommandBuilder(da)
Dim ds As New DataSet()
da.Fill(ds)
GridView1.DataSource = ds.Tables(0)
If n = 1 Then
If Not Page.IsPostBack Then
GridView1.DataBind()
End If
Else
GridView1.DataBind()
End If
everything works perfectly except that I need to position account attributes like name, position, ID number etc not horizontally but vertically. Something like this:
|NAME | John |
|SURNAME | Philips|
|POSITION| Manager|
and I can't find a way how to do it. Can anyone give me a hint in my problem please?
many thanks in advance.
If you want to present a detailed view of DB record, GridView is not the best choice (which is more suitable for tabular display of multiple records).
For your case take a look at DetailsView control

SqlDataSource.Select()? How do I use this? (ASP.net)

I'm trying to retrieve values using VB.NET from a SQL database. How do I use SqlDataSource.Select()? Is there a way to move the value to a variable that I can use for other things?
I know its kind of scattered and vague but that is the best I can do. I basically need to set a labels text to a value in a table.
This puts the result query in to a DataTable.
DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;
foreach (DataRow dr in dt.Rows)
{
// Do something here IE grab the value of the first column
value = dr[0];
}
Repying to last question in comment:
YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")
I was getting crazy trying to do this simple operation:
retrieving data from sqldatasource and put it into variables that I can manipulate.
At the end, Here the working behind code to do this for VB.NET:
Dim DV As New DataView()
Dim DataTable As New DataTable()
Dim SqlDataSource1 As New SqlDataSource()
Dim VALUE As String
SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * from Table"
DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
DataTable = DV.ToTable()
For Each riga As DataRow In DataTable.Rows
VALUE = riga("table_name").ToString
Next
the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.
ENJOY

Resources