How to add a counter column to a DataGrid

<asp:Datagrid id=’Datagrid1′ runat=’server’> <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:Label id=’Label1′ Runat =’server’ > <%#getCount%> </asp:Label> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:Datagrid> Code behind VB.NET Dim count As Integer protected function getCount() As Integer count = count + 1 Return count End Function Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Populate the Datagrid populating data from the database ’….. End Sub C# int count =0; protected int getCount() { count = count + 1; return count; } private void Page_Load(object sender, System.EventArgs e) { //Populate the Datagrid populating data from the database //…. }

How to do alphabetical paging in ASP.NET

Step 1: Create linkbuttons to display the alphabets at the footer of the datagrid Step 2: Add this buttons in the ItemCreated event . Use the CommandName and CommandArgument properties of the LinkButton to identify them Step 3: To handle the Paging , In the ItemCommand Event respond to action based on the CommandName and CommandArgument of LinkButton that caused event to be raised. <asp:DataGrid ShowFooter=’True’ OnItemCreated=’ItemCreated’ OnItemCommand=’ItemCommand’ id=’DataGrid1′ runat=’server’></asp:DataGrid> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’ Put user code to initialize the page here If Not Page.IsPostBack Then BindGrid(”) End If End Sub Sub BindGrid(ByVal stralpha As String) Dim cn As SqlConnection Dim da As SqlDataAdapter Dim ds As DataSet cn = New SqlConnection(‘Server=localhost;uid=sa;pwd=;database=pubs’) Dim strsql As String = ‘Select * from authors ‘ If stralpha = ” Then strsql = strsql Else strsql = strsql + ‘ where au_lname like’’ + stralpha + ‘%’’ End If da = New SqlDataAdapter(strsql, cn) ds = New DataSet da.Fill(ds, ‘Product’) If (ds.Tables(0).Rows.Count = 0) Then Response.Write(‘No Data Found’) DataGrid1.DataSource = Nothing DataGrid1.DataBind() Else DataGrid1.DataSource = ds DataGrid1.DataBind() End If End Sub Protected Sub ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) If e.Item.ItemType = ListItemType.Footer Then e.Item.Cells.Clear() Dim tc As TableCell = New TableCell tc.ColumnSpan = 2 e.Item.Cells.Add(tc) Dim lc As LiteralControl Dim lb As LinkButton Dim i As Integer For i = 65 To 65 + 25 lc = New LiteralControl lb = New LinkButton lc.Text = ‘ ‘ lb.Text = Chr(i) lb.CommandName = ‘alpha’ lb.CommandArgument = Chr(i) tc.Controls.Add(lb) tc.Controls.Add(lc) Next End If End Sub Protected Sub ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) If (e.CommandName = ‘alpha’) Then BindGrid(e.CommandArgument.ToString()) End If End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!Page.IsPostBack ) { BindGrid(”); } } void BindGrid(string stralpha) { SqlConnection cn; SqlDataAdapter da; DataSet ds; cn = new SqlConnection (‘Server=localhost;uid=sa;pwd=;database=pubs’); string strsql =’Select * from authors ‘; if (stralpha==”) { strsql = strsql ; } else { strsql = strsql + ‘ where au_lname like’’ + stralpha + ‘%’’; } da= new SqlDataAdapter (strsql,cn); ds= new DataSet (); da.Fill (ds, ‘Product’); if (ds.Tables [0].Rows.Count ==0) { Response.Write (‘No Data Found’); DataGrid1.DataSource=null; DataGrid1.DataBind (); } else { DataGrid1.DataSource =ds; DataGrid1.DataBind (); } } protected void ItemCreated(Object sender , System.Web.UI.WebControls.DataGridItemEventArgs e ) { if (e.Item.ItemType == ListItemType.Footer) { e.Item.Cells.Clear(); TableCell tc = new TableCell(); tc.ColumnSpan = 2; e.Item.Cells.Add(tc); LiteralControl lc; LinkButton lb; string s=” ; for (char c=’A’ ; c<= ’Z’ ; ) { lc = newLiteralControl (); lb = new LinkButton (); s = c.ToString() ; lc.Text =’ ‘; lb.CommandName =’alpha’; lb.CommandArgument= s; lb.Text=s; c =(char)((int)c +1) ; tc.Controls.Add(lb); tc.Controls.Add(lc); } } } protected void ItemCommand(Object source , System.Web.UI.WebControls.DataGridCommandEventArgs e) { if (e.CommandName == ‘alpha’ ) { BindGrid(e.CommandArgument.ToString ()); } }

I am binding the DataGrid to a datasource at runtime. After binding I want to populate a listbox with the column headers from the datagrid. How can I reference the column headertext for each column in the datagrid and then add them to my listbox

In the ItemDataBound Event write following code VB.NET Dim i As Integer = 0 If e.Item.ItemType = ListItemType.Header Then For i = 0 To e.Item.Cells.Count -1 Me.ListBox1.Items.Add(e.Item.Cells(i).Text) Next End If C# if (e.Item.ItemType== ListItemType.Header) { for(int i = 0; i <= e.Item.Cells.Count-1 ; i++) { this.ListBox1.Items.Add(new ListItem(e.Item.Cells[i].Text)); } }

How to force the max no. of characters in a multiline TextBox using RegularExpressionValidator

<asp:TextBox id=’TextBox1′ style=’Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 112px’ runat=’server’ TextMode=’MultiLine’></asp:TextBox> <asp:RegularExpressionValidator id=’RegularExpressionValidator1′ style=’Z-INDEX: 102; LEFT: 216px; POSITION: absolute; TOP: 112px’ runat=’server’ ErrorMessage=’MaxLength is 20′ ControlToValidate=’TextBox1′ ValidationExpression=’^\w{1,20} > </asp:RegularExpressionValidator>