DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Building a RESTful Service Using ASP.NET Core and dotConnect for PostgreSQL
  • Working With dotConnect for Oracle in ASP.NET Core
  • Implementing Cache Dependency in ASP.NET Core
  • Working With dotConnect for SQL Server in ASP.NET Core

Trending

  • How Trustworthy Is Big Data?
  • Streamlining Event Data in Event-Driven Ansible
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • AI, ML, and Data Science: Shaping the Future of Automation
  1. DZone
  2. Data Engineering
  3. Databases
  4. Creating a Responsive WebGrid in ASP.NET MVC

Creating a Responsive WebGrid in ASP.NET MVC

Ever wonder what it would take to integrate a responsive layout into .NET's WebGrid control using Bootstrap? Read on to find out the answer.

By 
Jonathan Danylko user avatar
Jonathan Danylko
·
May. 25, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
11.8K Views

Join the DZone community and get the full member experience.

Join For Free

picnic table

it's amazing how one reader's email can send me down the proverbial rabbit hole.

a reader asked if i knew of a way to integrate a responsive layout into a webgrid using bootstrap.

at first, i thought, "hey, this would make a great post to continue on with my webgrid series . this should be easy."

uh-huh.

i thought i could inherit from the webgrid and override some methods to make the webgrid a little more flexible for my needs.

as i mentioned before, nothing is ever simple or easy.

examining the webgrid

to make the webgrid responsive (specifically on a mobile device), i wanted to add a custom attribute to each <td> cell.

i know that you can have custom html attributes, but those are specific to the table itself. yet, if i want a custom attribute (<td data-label="column1">) attached to a table cell (td) or table row (tr), there isn't a way to achieve that with the current webgrid.

after examining the webgrid source code on github, i was even more startled.

i noticed a webgridrenderer cs and cshtml file. after looking through these two files, i realized they were compiled into the assembly.

hmm.

the only time the webgridrenderer was used was at the end of two htmlhelper methods: one called pager and the other called table .

the interesting part is that the webgridrenderer.cshtml is like a partial along with a long list of functions and helpers included in the file.

there's another issue i noticed with the webgrid "partial." when rendering the <td> element (or <tr> element for that matter), it's assuming that you are only going to add css classes to the element ( line 123 ).

<td@cssclass(style)>

this is kind of an issue.

i thought i would be able to squeeze additional attributes into that td, but unfortunately with this architecture, i won't be able to achieve it.

rewrite the webgrid?

we can attack this problem one of three ways. refactor the code:

  • programmatically - write code to create the table.
  • declaratively - use html and pass in the data/model.
  • not at all - use css to work around the issue.

personally, i think option 2 is a better way because it allows more flexibility with the code. also, if you have to make a change, you always have to compile. i would rather update the html and deploy that without compiling the entire project.

i understand why the webgrid was created that way. they wanted to include the html programmatically when you requested the webgrid assembly by using nuget. i get it. it has to be portable and nuget-able.

a quick approach is to use css to make it responsive and worry about the webgrid another day.

the simple approach

instead of writing a whole new webgrid (not yet, folks, hold on), the best approach is to use css to make our webgrid responsive.

we'll use the webgrid from our webgrid series . applying the css to the webgrid is very simple.

@@media screen and (max-width: 600px)
{
    .nav li { float: left }
    table, tbody, td, tr { display: block; border: none;}
    .table-bordered > tbody > tr> td { border: none;}
   tr { border-bottom: 1px solid #777}      thead, th { display: none }    table td:before {
        float: left;
        text-transform: uppercase;
        font-weight: bold;
        border: none;
    }

 td:nth-of-type(2):before { width: 200px; content: "user name";}
 td:nth-of-type(3):before { width: 200px; content: "first name"; }
 td:nth-of-type(4):before { width: 200px; content: "last name"; }
 td:nth-of-type(5):before { width: 200px; content: "last login"; }
}

the first line makes the toolbar a little aesthetically pleasing.

the next set of css classes makes the table body, table cell, and table row a block element with no borders.

since we are making our own headlines, let's remove them by "display: none"-ing them.

with every table cell in the table, we want it pulled to the left, bold type, and no border around it.

finally, we pick the nth column of a table cell and make the width 200 pixels and place the heading inside the cell.

once applied, we get this result.

screenshot of responsive webgrid results

our webgrid looks great on a mobile device now.

conclusion

today, we focused on making a responsive webgrid using css. this made the webgrid easier to work with by changing the presentation of grid instead of reworking the implementation of the webgrid.

did anyone make changes to the webgrid source code to use attributes on table cells or table rows?

post your comments in the sidebar to discuss further.

ASP.NET MVC Database ASP.NET

Published at DZone with permission of Jonathan Danylko, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a RESTful Service Using ASP.NET Core and dotConnect for PostgreSQL
  • Working With dotConnect for Oracle in ASP.NET Core
  • Implementing Cache Dependency in ASP.NET Core
  • Working With dotConnect for SQL Server in ASP.NET Core

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!