Archive for the ‘Controls’ Category
Dropdown List event not firing
Posted by: admin in .NET, Dropdown List on June 18th, 2010
Just make sure to set the AutoPostBack = true.
How to Print Datagrid in ASP.Net
To print the datagrid data alone, you need to encapsulate the datagrid using div element.
Example:
-
<script language="javascript" type="text/javascript">
-
function PrintDataGrid(elementname) {
-
var oDiv = document.getElementById(elementname);
-
var oprint_window = window.open('', '', 'letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
-
-
oprint_window.document.write(oDiv.innerHTML);
-
oprint_window.document.close();
-
oprint_window.focus();
-
oprint_window.print();
-
oprint_window.close();
-
}
-
</script>
-
-
-
<div id="DatagridData">
-
<asp:Datagrid>…</asp:Datagrid>
-
</div>
-
-
<asp:Button ID="btnPrint" runat="server" OnClientClick="javascript:PrintDataGrid('DatagridData');" />
or
-
<input id="Button1" type="button" value="button" onclick="javascript:PrintDataGrid('DatagridData')" />
How to search Node in Treeview Control
-
Private Function search(ByVal Nod As TreeNode, ByVal StrFind As String) As TreeNode
-
If Nod Is Nothing Then Nod = Me.Nodes(0)
-
Dim tmpNod As TreeNode
-
-
If String.Compare(Nod.Text, StrFind, True) = 0 Then
-
Return Nod
-
Else
-
For Each mNod As TreeNode In Nod.Nodes
-
tmpNod = search(mNod, StrFind)
-
If Not tmpNod Is Nothing Then
-
Return tmpNod
-
End If
-
Next
-
End If
-
Return Nothing
-
End Function
Usage:
-
dim nod as TreeNode
-
-
nod = search(me.MyTreeViewControl.Nodes(0), "My search")
How to remove parent node checkbox of the Treeview
Add this script to your common module
VB.NET
-
-
Public Const TVIF_STATE As Integer = &H8
-
Public Const TVIS_STATEIMAGEMASK As Integer = &HF000
-
Public Const TV_FIRST As Integer = &H1100
-
Public Const TVM_SETITEM As Integer = TV_FIRST + 63
-
-
Public Structure TVITEM
-
Public mask As Integer
-
Public hItem As IntPtr
-
Public state As Integer
-
Public stateMask As Integer
-
Public lpszText As String
-
Public cchTextMax As Integer
-
Public iImage As Integer
-
Public iSelectedImage As Integer
-
Public cChildren As Integer
-
Public lParam As IntPtr
-
End Structure
-
-
Public Sub RemoveCheckbox(ByVal nod As SiteTreeNode)
-
Dim tvi As TVITEM
-
tvi.hItem = nod.Handle
-
tvi.mask = TVIF_STATE
-
tvi.stateMask = TVIS_STATEIMAGEMASK
-
tvi.state = nod.Index << 12
-
SendMessage(nod.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, tvi)
-
End Sub
Then call the subroutine
Public Sub TreeNode_SetStateImageIndex(ByVal node As TreeNode, ByVal index
As Integer)
RemoveCheckbox(node)
End Sub
C#
-
-
using System.Runtime.InteropServices;
-
-
namespace Treeview___CheckBoxes
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
// Iterate over the root nodes, removing their checkboxes
-
for (int n = 0; n<treeView1.Nodes.Count; n++)
-
{
-
TreeNode node = treeView1.Nodes[n];
-
TVITEM tvItem = new TVITEM();
-
tvItem.hItem = node.Handle;
-
tvItem.mask = TVIF_STATE;
-
tvItem.stateMask = TVIS_STATEIMAGEMASK;
-
tvItem.state = 0;
-
IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvItem));
-
Marshal.StructureToPtr(tvItem, lparam, false);
-
SendMessage(this.treeView1.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
-
}
-
}
-
public const int TVIF_STATE = 0×8;
-
public const int TVIS_STATEIMAGEMASK = 0xF000;
-
public const int TV_FIRST = 0×1100;
-
public const int TVM_SETITEM = TV_FIRST + 63;
-
public struct TVITEM
-
{
-
public int mask;
-
public IntPtr hItem;
-
public int state;
-
public int stateMask;
-
[MarshalAs(UnmanagedType.LPTStr)]
-
public String lpszText;
-
public int cchTextMax;
-
public int iImage;
-
public int iSelectedImage;
-
public int cChildren;
-
public IntPtr lParam;
-
}
-
[DllImport(“user32.dll”)] static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
-
-
}
-
}
How to add object to a combobox
To add the object as an item to a combobox is pretty easy, it is the same as you are adding a string/numeric value.
Example:
-
Public class X
-
Public Name as String
-
Public Value as String
-
-
Public Sub New(StrName As String, StrValue As String)
-
Name = StrName
-
Value = StrValue
-
End Sub
-
End Class
-
-
To fill the combobox with the created object, let's say you have a combo box called cboX
-
dim Obj as X
-
For i as Integer = 0 to 10
-
cboX.Items.Add(New X("Name " & i.ToString(), "Value " & i.ToString())
-
Next
Then you need to set the DisplayMember property of the Combo box so that the Text displayed in the
combobox is not “namespace.X”.
-
cboX.DisplayMember = "Name"
How to Adjust DropdownWidth of the ComboBox
use this script to adjust the size of the combobox
private void AdjustWidthComboBox_DropDown(object sender, System.EventArgs e)
{
ComboBox senderComboBox = (ComboBox)sender;
int width = senderComboBox.DropDownWidth;
Graphics g = senderComboBox.CreateGraphics();
Font font = senderComboBox.Font;
int vertScrollBarWidth =
(senderComboBox.Items.Count>senderComboBox.MaxDropDownItems)
?SystemInformation.VerticalScrollBarWidth:0;
int newWidth;
foreach (string s in ((ComboBox)sender).Items)
{
newWidth = (int) g.MeasureString(s, font).Width
+ vertScrollBarWidth;
if (width < newWidth )
{
width = newWidth;
}
}
senderComboBox.DropDownWidth = width;
}
Add Custom Object to Combo box
Use Items.add (object) and make sure your object class is overriding the ToString()
Specified cast is not valid in WriteXml using Dataset
This exception appears when using WriteXml of datatable or dataset. This is also caused by when you create a datatable manually and you set the “defaultvalue” to one or more datacolumn. Remove the “defaultvalue” and it will be fine.
For more information on setting defaultvalue click here
A Generic error occured in GDI+
Source Control:
If this exception occurred in DataGrid Control, check if the displayed data is large. This might be the caused of the exception.
How to get Last Directory of OpenDialog box in .net
You can use the InitialDirectory property of the dialog box to get the last directory browsed.