Uncategorized

Create a Custom GroupBox for selecting multiple choice in ASP.Net and C#

Many a time , We need to create a control for selecting Multiple Inputs in our Web Applications .

I am gonna show a demo how we can create a GroupBox type thing to select multiple items and Get those values in server side .

Lets see an example …

First see the image below :

Custom GroupBox for selecting multiple choice in ASP.Net and C#

Custom GroupBox for selecting multiple choice in ASP.Net and C#

If our requirement is like – User wants to see the report for all the Tasks which are in Open and Close Status.
So User can easily select multiple options .

So Here is my mark up code below :::

<asp:GridView ID="gridTicketStatus" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <asp:CheckBox ID="selectCheckBox" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Id" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblStatusId" runat="server" Text='<%#Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Select Status">
                        <ItemTemplate>
                            <asp:Label ID="lblTaskStatus" runat="server" Text='<%#Bind("TaskStatus") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

Here I used a ASP.Net Grid which gets the status from the Database and Binds the Status.And I used another CheckBox , that’s how we can create any custom control . If we also need any DropDown inside it, we can easily add the DropDown with it as same way .

The Grid binding code in C# is like :::

DataTable dtStatus = TaskStatusGateway.GetAllTaskStatuses();
            gridTicketStatus.DataSource = dtStatus;
            gridTicketStatus.DataBind();

And after clicking the Button we can easily get all the selected Statuses by doing like below.

List<int> checkedStatus = new List<int>();

            if (gridTicketStatus.Rows.Count > 0)
            {
                for (int i = 0; i < gridTicketStatus.Rows.Count; i++)
                {
                    if (((CheckBox) (gridTicketStatus.Rows[i].FindControl("selectCheckBox"))).Checked)
                    {
                        int id = Convert.ToInt32(((Label) gridTicketStatus.Rows[i].FindControl("lblStatusId")).Text);
                        checkedStatus.Add(id);
                    }
                }
            }

So we are done .

Cheers 🙂

Standard
Uncategorized

Create Dynamic DataTable in C#

Many a time , we need to create DataTable dynamically inside our C# Code . So this is the easiest way how we can create DataTable dynamically and add data to it .

DataTable dtDatabaseInfo = new DataTable("tblData");
            dtDatabaseInfo.Columns.Add("ID", typeof(string));
            dtDatabaseInfo.Columns.Add("Destination", typeof(string));
            dtDatabaseInfo.Columns.Add("ServerName", typeof(string));
            dtDatabaseInfo.Columns.Add("sDB_Name", typeof(string));
            dtDatabaseInfo.Columns.Add("DbUserName", typeof(string));
            dtDatabaseInfo.Columns.Add("DbPassword", typeof(string));

            DataRow row = dtDatabaseInfo.NewRow();

            row["ID"] = "M001";
            row["Destination"] = "MyDestination";
            row["ServerName"] = "MyLocalServer";
            row["sDB_Name"] = "MyDatabase";
            row["DbUserName"] = "MyUser";
            row["DbPassword"] = "MyPassword";
            dtDatabaseInfo.Rows.Add(row);

Standard