1

I want to display data in my own format from Mysql database connecting through ASP.NET. Currently I'm using MySQLDataGrid to display values in table format.

My code is

myConnection = New MySqlConnection("server=localhost; user id=root; password='';       database=test; pooling=false;")
strSQL = "SELECT * FROM tablename;"
myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
myDataSet = New Dataset()
myDataAdapter.Fill(myDataSet, "mytable")
MySQLDataGrid.DataSource = myDataSet
MySQLDataGrid.DataBind()
and asp code is:
<asp:DataGrid id="MySQLDataGrid" runat="server" />

its displaying all columns in Grid format. But i actually wanted to display in HTML table format with some included/exclude fields. like below.

UserName : City : Address: etc...

Can I have a simple example code on how to do it?

1 Answer 1

0

By default, columns are auto-generated corresponding to the binded datasource. You can disable this with the property AutoGenerateColumns by setting it to False:

<asp:DataGrid ... AutoGenerateColumns="False">

Then you can define yourself the columns you'd like to show under the <columns/> tag

<asp:DataGrid ... >    
    <Columns>
        <asp:BoundColumn 
            HeaderText="Username" 
            DataField="UserName"/>

        <asp:BoundColumn 
            HeaderText="City" 
            DataField="City"/>

        ...

    </Columns>
</asp:DataGrid>

There a few types of columns:

BoundColumn Displays a column bound to a field in a data source. It displays each item in the field as text. This is the default column type of the DataGrid control.

ButtonColumn Displays a command button for each item in the column. This allows you to create a column of custom button controls, such as Add or Remove buttons.

EditCommandColumn Displays a column that contains editing commands for each item in the column.

HyperLinkColumn Displays the contents of each item in the column as a hyperlink. The contents of the column can be bound to a field in a data source or static text.

TemplateColumn Displays each item in the column following a specified template. This allows you to provide custom controls in the column.

The documentation page for the DataGrid show a bunch of example on how to use the different types of columns (button columns, add a checkbox...)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.