Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

[PLAIN]Totally confused with datagrid. [solved][/PLAIN]

 

Hello.

I have the following:

 

Public Sub populateGrid(ByVal MyTable As String, ByVal DG As DataGrid)

Dim ds As New DataSet

Dim sqlstr As String = "SELECT * FROM " & MyTable

OpenDB()

Dim dstable As New OleDb.OleDbDataAdapter(sqlstr, MyConnection)

dstable.Fill(ds, MyTable)

DG.DataSource = ds.DefaultViewManager

DG.DataMember = MyTable

CloseDB()

End Sub

 

and this when the form loads

 

 

Dim mydata As New DataGridTableStyle

mydata.MappingName = "id"

populateGrid("user_security", dgusers)

mydata.GridColumnStyles(0).Width = 0

dgusers.TableStyles.Add(mydata)

 

 

now it's failing on the hidding part in the last 2 rows

I can't figure out how to hide a column on a Datagrid

 

populateGrid("user_security", dgusers)

 

is filling the datagrid in the form (dgusers = the gridname)

and had 4 columns

 

0 = ID

1 = Name

2 = Psw

3 = Level

 

I want to hide Column 0

which I though was with this part

 

mydata.GridColumnStyles(0).Width = 0

dgusers.TableStyles.Add(mydata)

 

But that gives me a error.

I've read several docs and forums but everytime it's something different.

Could someone tell me what I'm doing wrong?

Thank you so much...

Edited by mrdutchie
Posted

hi

 

there are some steps to

 

well first you must clear the tableStyle

 

dgusers.TableStyles.Clear()

 

 

then mapping the table name after declaring new table

 

mydata.MappingName = dataSet.table.TableName

 

 

then you must declare the DataGridTextBoxColumn

 

that you want to display it into your dataGrid

 

No need to hide column in this way

 

after declaring it

 

Dim mydataGrid As New DataGridTextBoxColumn

With grdColStyle1

.MappingName = "LeaveKind"

.HeaderText = "vacation"

.Width = 90

.NullText = ""

End With

 

now add this column to the tableStyle

 

mydata.GridColumnStyles.Add(mydataGrid)

 

dgusers.TableStyles.Add(mydata)

 

if this not Cool answer then

 

you can hide the column from dataSet then

 

bound the dataGrid to that table after hiding that column

 

something like this

 

Ds1.Tables("T").Columns("c0").ColumnMapping = MappingType.Hidden

 

DG.DataSource = Ds.Tables("T")

 

I hope them help :-\

Gary Says: To be a good programmer, you must be good at debugging
Posted

I did it in C# but might give you some picture... this is different to loyal's example.

 

 

dataGrid1.SetDataBinding( datasource, "TableName" );

 

DataGridTableStyle dGridTS = new DataGridTableStyle();

dGridTS.MappingName = "TableName";

 

DataGridTextBoxColumn HiddenTextCol1 = new DataGridTextBoxColumn();

HiddenTextCol1.MappingName = "FieldName";

HiddenTextCol1.HeaderText = "";

HiddenTextCol1.Width = 0;

HiddenTextCol1.NullText = "";

dGridTS.GridColumnStyles.Add(HiddenTextCol1);

 

dataGrid1.TableStyles.Add(dGridTS);

 

 

Hope it helps (In fact, I think loyal has already given a good example too)

Be you his eunuch, and your mute I'll be:

When my tongue blabs, then let mine eyes not see.

Posted (edited)

Woww.. After spending 6 hours on this last night I still don't understand and can't get it to work.

I guess I need a little bit more help, to understand this part.

 

The way I am filling my Datagrid, is that the wrong way?

 

I have a Access Database with the table "User_sec"

with 3 fields "ID" "User" "Psw" and "Level"

 

and a Datagrid that's called "DGUSERS"

I want to show all except "ID" that one needs to be hidden.

 

 

I changed the code a little bit, so you can see the full thing

mypath is the path to the database.

 

 

 

Public Sub fillgrid()

Dim MyConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyPath)

Dim dg As DataGrid = dgusers

Dim mytable As String = "user_security"

Dim mydata As New DataGridTableStyle

Dim ds As New DataSet

Dim sqlstr As String = "SELECT * FROM " & mytable

If Not MyConnection.State = ConnectionState.Open Then MyConnection.Open()

Dim dstable As New OleDb.OleDbDataAdapter(sqlstr, MyConnection)

dstable.Fill(ds, mytable)

dg.DataSource = ds.DefaultViewManager

dg.DataMember = mytable

If Not MyConnection.State = ConnectionState.Closed Then MyConnection.Close()

end Sub

 

What do I need to change in the existing code, or do I need to rewrite it totally?

I must say can I intergrate from what's writting above, or do I nee to start from scratch and forget about this code to fill a datagrid that I wrote.

Edited by mrdutchie
Posted

ALLRIGHT... :)

 

Found it... it's just 1 line

 

ds.Tables(mytable).Columns(0).ColumnMapping = MappingType.Hidden

 

 

No need for all the other code.....

 

Thanks guys... :cool: :cool:

Posted

The .hidden works great, but then whatever was in that column I can't get anymore..

ie. I have 4 columns

ID Name PSW LEVEL

 

I hide the ID column but when I click on the rows in my

datagrid I can't get the value anymore from Column (0)

since the .hidden option moved all the columns.

So

column(1) becomes Column(0)

column(2) becomes Column(1)

etc.

 

Or is there a way to get those values?

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...