• http://www.techneurons.com/career/
  • experienced programming consultants for hire !!!

    Contact Now

    ASP.NET Support

    CheckBox field in GridView

    Posted by surendra on Mar 21, 2010
    how to create CheckBoxes in gridview .....
    it means where to create like in columns tag or asp:databound field.
    pl send me the answer.......
    Replied by consultsarath on Mar 21, 2010
    <p>GridView allows you to add a CheckBox column using the <strong>&lt;asp:CheckBoxField&gt;</strong> control. This is a data bound field control, and it renders as a single checkbox in the gridview cell. The DataField requires to be Boolean or Boolean equivalent field.</p> <div><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">GridView</span> <span style="color: red">runat</span><span style="color: blue">=&quot;server&quot;</span> <span style="color: red">ID</span><span style="color: blue">=&quot;gridView&quot;&gt;</span></span> <span style="font-family: 'Courier New'; font-size: 9pt"><span style="color: blue">&lt;</span><span style="color: #a31515">Columns</span><span style="color: blue">&gt;</span></span></div> <div><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="background-color: #ffff99"><strong><span style="font-family: 'Courier New'; font-size: 9pt"><span style="color: blue">&lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">CheckBoxField</span> <span style="color: red">DataField</span><span style="color: blue">=&quot;IsSelected&quot;&gt;</span></span> <span style="font-family: 'Courier New'; font-size: 9pt"><span style="color: blue">&lt;</span><span style="color: #a31515">ItemStyle</span> <span style="color: red">Width</span><span style="color: blue">=&quot;20px&quot;</span> <span style="color: blue">/&gt;</span></span></strong></span></div> <div><strong><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="background-color: #ffff99"><span style="color: blue">&lt;/</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">CheckBoxField</span> <span style="color: blue">&gt;</span></span></span></strong></div> <div><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">Columns</span><span style="color: blue">&gt;</span></span></div> <div><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">GridView</span><span style="color: blue">&gt;</span></span></div> <p>You can load data into the gridview using the regular databinding methods. On postback or for custom changes, you can access the CheckBox for programming using a code like this:</p> <div style="margin: 0cm 0cm 10pt"><span style="line-height: 115%; font-family: 'Courier New'; color: blue; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CType</span><span style="line-height: 115%; font-family: 'Courier New'; font-size: 9pt">(gridView.Rows(<span style="color: #ff0000">rowIndex</span>).Cells(<span style="color: #ff0000">columnIndex</span>).Controls(0), CheckBox).Checked = <span style="color: blue">False</span></span></div> <p>The above example uses a built-in CheckBox field. There are greater possibilities if you use TemplateField and create an ItemTemplate containing the CheckBox. You can add client-side javascripts and many more customizations if you do as a TemplateField. The Source View of the GridView when a TemplateField is used to render the checkbox would be like this:</p> <div style="line-height: normal"><span style="font-family: 'Courier New'; color: blue; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-family: 'Courier New'; color: #a31515; font-size: 9pt">asp</span><span style="font-family: 'Courier New'; color: blue; font-size: 9pt">:</span><span style="font-family: 'Courier New'; color: #a31515; font-size: 9pt">GridView</span><span style="font-family: 'Courier New'; font-size: 9pt"> <span style="color: red">runat</span><span style="color: blue">=&quot;server&quot;</span> <span style="color: red">ID</span><span style="color: blue">=&quot;gridView&quot;&gt;</span></span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">Columns</span><span style="color: blue">&gt;</span></span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">TemplateField</span> <span style="color: blue">&gt;</span></span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">ItemStyle</span> <span style="color: red">Width</span><span style="color: blue">=&quot;20px&quot;</span> <span style="color: blue">/&gt;</span></span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">ItemTemplate</span><span style="color: blue">&gt;</span></span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">CheckBox</span> <span style="color: red">runat</span><span style="color: blue">=&quot;server&quot;</span> <span style="color: red">ID</span><span style="color: blue">=&quot;chkSelected&quot;</span>&nbsp;<br /> <span style="color: red">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Checked</span><span style="color: blue">='</span><span style="background: yellow">&lt;%</span># Eval(&quot;IsSelected&quot;) <span style="background: yellow">%&gt;</span><span style="color: blue">'</span> <span style="color: blue">/&gt;</span></span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">ItemTemplate</span><span style="color: blue">&gt;</span></span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">TemplateField</span><span style="color: blue">&gt;</span></span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">Columns</span><span style="color: blue">&gt;</span></span></div> <div style="line-height: normal"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">GridView</span><span style="color: blue">&gt;</span></span></div> <p>&nbsp;In this case, we used named controls, it is quite flexible to access it on the code-behind.</p> <div style="margin: 0cm 0cm 10pt 36pt"><span style="line-height: 115%; font-family: 'Courier New'; color: blue; font-size: 9pt">CType</span><span style="line-height: 115%; font-family: 'Courier New'; font-size: 9pt">(gridView.Rows(<span style="color: #ff0000">rowIndex</span>).FindControl(<span style="color: #a31515">&quot;rowIndex&quot;</span>), CheckBox).Checked = <span style="color: blue">False</span></span></div>
    We are experts in Cloud Computing Technologies. We can assist you to build high scalable business applications using Amazon Web Services (Amazon EC2, Amazon S3, Amazon SES, SNS, CloudFront), Windows Azure Platforms - Windows Azure and SQL Server Azure, Google App Engine using Python and Django Framework. We are Expert Programming Consultants available at affordable rates per hour. We work on several technologies - .NET, Python, Google App Engine, PHP, Windows Azure, Amazon Web Services ...