hi guys i have a table in a database with data of which i want to display in a tabular format. In addition i want also to be able to edit a particular column of the displayed tabular data but the editing will only allow for the rentering of numbers, strings and -, / foreign characters.

i heard about a list view but not too sure!!!!

Recommended Answers

All 8 Replies

Sounds like a gridview would be easier to use. Are you using Visual Studio? While I usually recommend coding by hand, its easy to drop these controls on a page and use the wizards to fully configure the objects without having to know all of the details in the back-end.

If you are just interested in displaying data in a table form, I find the gridview to be easy to use and configure. You can even enable editing so that each line with its associated fields can be modified.

my issue with the gridview how would you set it up so that the user only puts in numeric or string values when editing and i am using visual studio 2010. and i prefer to display the data programmatically.

So, if you were using the controls, in the gridview properties, turn the field into a template. Then you could use the various Validation controls on each of the input type of elements.

With regard to validation, if you do this only programatically, I suspect that you'll need to include your own JavaScript to do the validation. Unless you want to validate the entries after the user submits and check it at the server.

Can you recommend a tutorial that i can reference?

You can use the template field as suggested by JorgeM. If you use the AjaxControlToolkit then you could use the FilteredTextBoxExtender to limit what the user is able to enter.

Regardless of how you handle it on the client side you should always validate the input on the server side since it is possible for users to change what is being sent to the server between the time when it's validated on the client side and when it actually gets sent.

A gridview column often is just made of BoundFields and looks similar to

<Columns>
    <asp:BoundField DataField="ColumnOne" SortField="ColumnField" />
</Columns

When using a template field that would instead look like

<Columns>
    <asp:TemplateField HeaderText="Column One" SortField="ColumnOne">
        <ItemTemplate>
            <asp:Label ID="label_ColumnOne" runat="server" Text='<%# Eval("ColumnOne") %>' />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="textbox_ColumnOne" runat="server" Text='<%# Bind("ColumnOne") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>
</Columns>

The ItemTemplate is what is displayed by default. The EditItemTemplate gets displayed when you select "edit" for that row. You can add as many controls into a template field as makes sense for your need. So you can add a validator control to the textbox, or a filter control, or whatever.

Here's a link for a tutorial on Using TemplateFields In The GridView Control

   private void GetDataFromUserSelection(int opt, string areaname, long areaid) {


        AEERR.Visible = false;
    try
        {
        DBCON dbcon = new DBCON("masterConnectionString");
        if (dbcon.startup() == false)
            {
            AEERR.Visible = true;
            AEERR.Text = "Error occured at the database level, connection failed. Please try again or contact your administrator";
            }
        else {

        localsqlconnect = dbcon.getConection();
        dbcon.initStoredProcedure("GetZones");
        localsqlcmd = dbcon.getCommand();
        localsqlcmd.Parameters.Add("@idnum", SqlDbType.BigInt).Value = areaid;
        localsqlcmd.Parameters.Add("@areaname", SqlDbType.NVarChar).Value = areaname;
        localsqlcmd.Parameters.Add("@option", SqlDbType.Int).Value = opt;
        localsqlreader = localsqlcmd.ExecuteReader();

        if (localsqlreader.HasRows == false)
            {
            AEERR.Visible = true;
            AEERR.Text = "The table which houses the data appears to be empty. Please try again or contact your administrator";
            }

        else {

        dt.Load(localsqlreader);
        ListView1.DataSource = dt;
        ListView1.DataBind();

        //while (localsqlreader.Read() == true)
        //    {

        //    dt.Load(localsqlreader, 0);


        //    ListView1.DataSource = dt;
        //   ListView1.DataBind();
        //    }

        localsqlreader.Close();
        dbcon.CloseConnection();

            }



            }
        }

    catch (Exception ex) {
    AEERR.Visible = true;
    AEERR.Text = "Error occured at the database level, connection failed. Please try again or contact your administrator";
      }


        }

I developed the code above to display the data in the list view instead of a gridveiw my challenge is whenere the use clicks edit....i am having a ton of errors....Can you provide some gudiance on the editing maybe a tutorial reference

What are the errors you are getting? Can you provide the code for the edit event?

Hi guys i was able to fixed the issue so i am attaching the events for future reference.

 protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
        {
        if (rdbid.Checked == true) {
        ListView1.EditIndex = e.NewEditIndex;
        GetDataFromUserSelection(1, "", long.Parse(Helper.temp.ToString())); 
            }
        else if (rdbarea.Checked == true)
            {
            ListView1.EditIndex = e.NewEditIndex;
            GetDataFromUserSelection(2, Helper.temp.ToString(), 0);
            }
        else { 
              ListView1.EditIndex = e.NewEditIndex;
              GetDataFromUserSelection(0, "", 1);  //loads the full zones list 
            }


        }

    protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {


        if (rdbid.Checked == true)
            {
            ListView1.EditIndex = -1;
            GetDataFromUserSelection(1, "", long.Parse(Helper.temp));
            }
        else if (rdbarea.Checked == true)
            {
            ListView1.EditIndex = -1;
            GetDataFromUserSelection(2, Helper.temp.ToString(), 0);
            }
        else
            {
            ListView1.EditIndex =-1;
            GetDataFromUserSelection(0, "", 1);  //loads the full zones list 
            }

        }

    protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
        string tempstr;
        Label lbl = (ListView1.Items[e.ItemIndex].FindControl("IDLabel1")) as Label;
        TextBox txt2 = (ListView1.Items[e.ItemIndex].FindControl("txtareaname")) as TextBox;



        if (txt2.Text.Length <= 0)
            {
            AEERR.Visible = true;
            AEERR.Text = "Site Area Name Cannot be left blank";
            }
        else
        {
                    tempstr = Helper.IsAreaExists(txt2.Text.ToUpper()).ToUpper();

                    if (tempstr == txt2.Text.ToUpper())
                        {
                        AEERR.Visible = true;
                        AEERR.Text = "Area Name cannot be changed because it already exists";
                        ListView1.EditIndex = -1;
                        tempstr = null;
                        }
                    else if (tempstr == "")
                        {
                        tempstr = null;
                        Response.Clear();
                        Response.Write("connection failed, database error, please contact your administrator immediately");
                        Response.End();
                        return;
                        }
                    else {

                                        if (UpdateRecordQuery(long.Parse(lbl.Text), txt2.Text.ToUpper()) == false)
                                            {
                                            AEERR.Visible = true;
                                            AEERR.Text = "Error occurred in the database, the record was not changed.  Please try again or contact  your administrator";
                                            }

                                        else
                                            {
                                            AEERR.Visible = true;
                                            AEERR.Text = "Area Name Successfully Updated";
                                            Helper.AppRecord(Convert.ToInt64(Helper.base64Decode(Request.Cookies["SMAAUserId"].Value)), "Edit Area: The Area Name was edited to reflect " + txt2.Text.ToUpper() + " It is attached to the ID Number " +  lbl.Text);
                                            ListView1.EditIndex = -1;
                                            }




                                        }


            }


        if (rdbid.Checked == true)
            {
            GetDataFromUserSelection(1, "", long.Parse(lbl.Text));
            LoadData(1);
            }
        else if (rdbarea.Checked == true)
            {
            GetDataFromUserSelection(2, txt2.Text.ToUpper(), 0);
            LoadData(2);
            }

        else
            {
            GetDataFromUserSelection(0, "", 1);
            LoadData(0);
            }        
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.