Posts

Donate

DataGridView ComboBox Cascade In Windows Forms

Image
Good day to all! Here's an example of how to perform a combobox lookup or cascade using two DataGridViewComboBoxColumns inside a DataGridView control Combo Lookup in DGV . The solution presented is in VB.NET, so I decided to create a C# equivalent for this. In form load event, populate two DataTables for Roles and Employees. Each employee is assigned to a specific role. private void Form1_Load( object sender, EventArgs e) { DataGridView1.Rows.Add(); dtRole.Columns.Add( "RoleID" ); dtRole.Columns.Add( "RoleName" ); dtRole.Rows.Add(1, "Admin" ); dtRole.Rows.Add(2, "Instructor" ); dtRole.Rows.Add(3, "Utility" ); dtEmployee.Columns.Add( "RoleID" ); dtEmployee.Columns.Add( "EmployeeID" ); dtEmployee.Columns.Add( "EmployeeName" ); dtEmployee.Rows.Add(1, 1, "Sam" ); dtEmployee.Rows.Add(1, 2, "Nicole" ); dtEmployee.Rows.Add(2, 3, "Donald" ); dtEmployee.Rows.Add(

DataGridViewComboBoxColumn Show Dropdown In Single Click Instead Of Double Click

Hi, When adding DataGridViewComboBoxColumn control to a DataGridView control, the dropdown shows when you double click instead of single click. The common fix to this is to set the EditMode property to EditOnEnter . Given that you don't want to alter the default settings of the DataGrid control and you want to handle it through code,I found the solution in this website DATAGRIDVIEWCOMBOBOXCOLUMN REQUIRES MULTIPLE CLICKS TO SELECT AN ITEM from a comment made by a developer. However, there's a slight issue in the code provided since a column index returned might have a -1 index and this will cause an Unhandled Exception . The revision made is to add a condition that will check if the ColumnIndex of the DataGridView cell is greater than or equal to 0. private void DataGridView1_CellClick( object sender, DataGridViewCellEventArgs e) { DataGridView grid = (DataGridView)sender; if (e.ColumnIndex >= 0) { if (grid.Columns[e.ColumnIndex].Name == "Role" || grid.

Login To Facebook Account Using C#, Webbrowser And LINQ

Hi, Here's how to login to your facebook account using Webbrowser control and LINQ in Windows Forms. The input elements are checked through their attributes like "email" for email fields, "password" for password fields and "submit" for submission control. These codes are inside the DocumentCompleted event. C# code WebBrowser1.Document.GetElementsByTagName( "input" ).Cast<HtmlElement>().FirstOrDefault(t => t.GetAttribute( "type" ) == "email" ).SetAttribute( "value" , "your email address" ); WebBrowser1.Document.GetElementsByTagName( "input" ).Cast<HtmlElement>().FirstOrDefault(t => t.GetAttribute( "type" ) == "password" ).SetAttribute( "value" , "your user name" ); WebBrowser1.Document.GetElementsByTagName( "input" ).Cast<HtmlElement>().FirstOrDefault(t => t.GetAttribute( "type" ) == "submit" ).I

Inline-Block Elements Not Filling Up The Entire Width Of Div Container Using Width Percentage

Image
Hello, I encountered this issue several weeks ago and decided to put the solution here. This pertains to inline-block elements not occupying the entire width of the container. Each element's width is set using percentage instead of pixels. After doing some research, I found a solution here: Display Inline-Block with Widths as Percent with the solution to set the font-size of the container element to 0 and add style box-sizing to border-box. For the child elements, set the default font size. CSS Code .container { margin : 0 0 1em 0; border : 2px solid black; padding : 1em; font-size : 0; box-sizing: border -box; } nav { vertical-align : top ; display : inline - block ; width : 25%; word-wrap: break-word; background-color : lightgray; font-size : 16px; } .column { vertical-align : top ;

WPF DataGrid Set RowBackground Using AlternationCount And DataTriggers

Hi, There was a question raised on how to set the WPF DataGrid RowBackground with AlternationCount (alternating row colors) and DataTriggers without overriding the Alternation count. The fix is to declare a DataGrid.Style that contains settings for RowBackGround and AlternationCount. And in your DataGrid.RowStyle, define the DataTriggers to highlight RowBackground based on a given Value. <DataGrid x:Name= "dgEmployees" HorizontalAlignment= "Left" Margin= "0,0,0,0" VerticalAlignment= "Top" Height= "346" RowHeaderWidth= "0" AutoGenerateColumns= "False" CanUserAddRows= "False" CanUserResizeColumns= "False" CanUserDeleteRows= "False" ScrollViewer.CanContentScroll= "True" VerticalScrollBarVisibility= "Visible" HeadersVisibility= "Column" IsReadOnly= "True" > <DataGrid.Res

Read SQL Server XML Data Type Column In C#.NET

Image
Hi all, In this demo, I have dummy XML files saved to an XML column in a table. The files have the same structure of nodes, except that some files have altered price value of 104.95. The is the structure of the dummy XML file. <catalog> <book id= "bk101" > <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description> An in-depth look at creating applications with XML. </description> </book> <book id= "bk102" > <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description> A former architect battles corporate zombies, an ev

XML parsing: line 1, character 38, unable to switch the encoding

Hello all, Given the task at hand that your going to insert XML data into an XML column in SQL Server and you encounter this error "XML parsing: line 1, character 38, unable to switch the encoding", it seems the insertion of XML data failed due to this line here: <?xml version="1.0" encoding="utf-8" ?> . After doing some research, I found a tutorial on how to avoid unicode issues when inserting XML data into an XML column in SQL Server which is the basis of the solution. I just change the Encoding of the stream to UTF8 to match the encoding of the XML file. using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[ "DefaultConnection" ].ConnectionString)) { SqlCommand command = new SqlCommand( "Insert into XMLTable(name, xmlData) values (@name, @xmlData)" , con); con.Open(); string xmlFile = File.ReadAllText(location); using (MemoryStream stream = new MemoryStream()) { using (StreamWrite

Seed Roles And Users To An Existing Database In ASP.NET MVC 5 Using Identity

Image
Hello, In this tutorial, I will demonstrate seeding roles and users to an existing database in an ASP.NET MVC 5 application using Identity framework. For simplicity, I will use a Northwind. This database doesn't have membership tables at all. To begin with, accomplish the steps below: 1. Create an ASP.NET MVC application and then change web.config connectionStrings element to connect to an existing database (Northwind as my example). 2. Under Package Manager Console type PM> Enable-Migrations 3. Under Package Manager Console type PM> Add-Migration ASPMembership * This will create a file Timespan_ASPMembership.cs inside Migrations folder with scripts to create Membership Tables such as AspNetUsers, AspNetRoles and etc. 4. Under Package Manager Console type PM> Update-Database * This will add membership tables to Northwind database. 5. To seed Roles and Users, create a class SeedRolesAndUsers.cs inside Models folder. This class referenc

The term 'Enable-Migrations' is not recognized as the name of a cmdlet

Hello, When I started creating an ASP.NET MVC application in VB.NET and decided to add Membership tables using the Enable-Migrations command in Package Manager Console, I encountered this error 'The term 'Enable-Migrations' is not recognized as the name of a cmdlet'. In C#, I haven't encountered this issue. So, what I did was to Rebuild the project, and then restart Visual Studio IDE. Eureka,the problem was fixed. :-) Cheers! :)

How To Set WPF DataGridCell And DataGridRow Background Color Using Triggers

Hi, There was a question on the forums on how to set the background color of DataGridCell or DataGridRow using XAML without code. I always thought that the solution will be to use code using IValueConverter. After doing some searching on MSDN and google, the answer is straightforward using Triggers. To set the color of DataGridRow, you set the DataGrid.CellStyle just below the DataGrid markup. <DataGrid Grid.Row= "0" Grid.Column= "0" AutoGenerateColumns= "False" CanUserAddRows= "False" Name= "dgStudents" > <DataGrid.CellStyle> <Style TargetType= "{x:Type DataGridCell}" > <Style.Triggers> <DataTrigger Binding= "{Binding Age}" Value= "28" > <Setter Property= "Background" Value= "Gray" ></Setter> </DataTrigger> </Style.Triggers> </Style> </DataGrid.CellStyle> <DataGrid.Columns>

Donate