Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys, i have a gridview, that using boundfield inject from asp.net, also hardcode from C# IDE, first problem is everytime i load data into gridview in page load event, the field column always adding twice from it's original, example i select field a,b,c,d but bwcause of the postback the field shown in gridview is become a,b,c,b,c,d and this problem fixed when i put the eventhandler name into if(!ispostback){}, but this problem come again when i execute searching data from table. i has try to find the solution but still no luck, please everybody helm me..

this is some code from me :
C#
SqlConnection tarikdatakondisi1 = new SqlConnection(StartKoneksiDatatable());

                    // buka koneksi kedalam datatable
                    tarikdatakondisi1.Open();

                    SqlDataAdapter sda1 = new SqlDataAdapter("select BillingNo, deskripsi, customername, remarks from mastertablebilling where tablestatusId = 1 and billingstatusId = 1", tarikdatakondisi1);
                    DataTable dt1 = new DataTable();
                    sda1.Fill(dt1);

                    // masuk ke kondisi dimana datatable adakah atau tidak
                    if (dt1 != null)
                    {
                        if (dt1.Rows.Count > 0)
                        {
                            // melakukan perhitungan jumlah row kolom untuk dijadikan materi dan diinsert berapa banyak row kolom yang dibutuhkan dengan looping tiap field yang di select
                            for (int i = 0; i < dt1.Columns.Count; i++)
                            {
                                // assign boundfield sebagai array variable kolom space(ruang)
                                BoundField boundField = new BoundField();

                                // kondisi sebaliknya jika ternyata ada field tersebut, maka tetapkan langsung headertextnya
                                boundField.DataField = dt1.Columns[i].ColumnName.ToString();

                                // buat kondisi jika ternyata ada field yang bernama tersebut, maka prosedural penetapan secara otomatis dilewatkan, karena sudah ada dari hardcode
                                if (dt1.Columns[i].ColumnName.ToString() != "BillingNo")
                                {
                                    if (dt1.Columns[i].ColumnName.ToString() == "deskripsi")
                                    {
                                        boundField.HeaderText = "Deskripsi";
                                    }
                                    if (dt1.Columns[i].ColumnName.ToString() == "customername")
                                    {
                                        boundField.HeaderText = "Customer";
                                    }
                                    if (dt1.Columns[i].ColumnName.ToString() == "remarks")
                                    {
                                        boundField.HeaderText = "Remarks";
                                    }
                                    // asignment field yang sudah didapat hasil tangkapan ke boundfield untuk dijadikan row kolom sesuai dengan headertext yang sudah dibuat
                                    gridviewformindexteknis.Columns.Add(boundField);
                                }
                            }
                        }
                    }
                    // lempar data tarikan ke row grid
                    gridviewformindexteknis.DataSource = dt1;
                    gridviewformindexteknis.DataBind();
                    tarikdatakondisi1.Close();
}

this is from my gridview ASP.Net code :
C#
<td><asp:Panel ID="panelgdformindeteknis" runat="server" style="overflow:auto" Width="100%">
                <asp:GridView ID="gridviewformindexteknis" runat="server" 
                    AutoGenerateColumns="false" BackColor="White"
                BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
                    AllowPaging="false" OnPageIndexChanging="PageIndexChanging" 
                    style="overflow:auto" Width="100%">
                <footerstyle backcolor="#B5C7DE" forecolor="#4A3C8C" />
                <rowstyle backcolor="#E7E7FF" forecolor="#4A3C8C" />
                <pagerstyle backcolor="#E7E7FF" forecolor="#4A3C8C" horizontalalign="Right" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                <alternatingrowstyle backcolor="#F7F7F7" />
                <columns>
                <asp:HyperLinkField HeaderText="Nomer Billing" Target="_blank" DataNavigateUrlFields="BillingNo" DataTextField="BillingNo" DataNavigateUrlFormatString="~/detailBillingTek.aspx?BillingNo={0}" />
                </columns>

please anyone any can help me?
Posted
Updated 27-Oct-13 20:53pm
v3
Comments
CodeBlack 28-Oct-13 2:46am    
can you update your question with code ?
haritz.aryandra 28-Oct-13 3:54am    
i has give the code, do you know what wrong with the code?
CodeBlack 28-Oct-13 4:04am    
why do you use for loop instead of directly assigning datasource and adding bound field in grid itself ?
haritz.aryandra 28-Oct-13 4:11am    
because i has condition from searching the data, it use radiobutton as the filter also the combo and textbox used for input criteria from the combo, do you get what i mean?
haritz.aryandra 28-Oct-13 4:13am    
i have 3 radio button (a,b,c) each radiobutton shown trigger more field than others, exp. : rbo a only shown 4 field, rbo b shown 5, etc..

As your binding code only retrieves data from database and bind it to grid view, I suggest to bind it as mentioned below :

Design Page:
XML
<asp:GridView ID="gridviewformindexteknis" runat="server"
        AutoGenerateColumns="false" BackColor="White"
        BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3"
        AllowPaging="false" OnPageIndexChanging="PageIndexChanging"
        Style="overflow: auto" Width="100%">
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
        <Columns>
            <asp:BoundField DataField="deskripsi" HeaderText="Deskripsi" />
            <asp:BoundField DataField="deskripsi" HeaderText="Deskripsi" />
            <asp:BoundField DataField="customername" HeaderText="Customer" />
            <asp:BoundField DataField="remarks" HeaderText="Remarks" />
            <asp:HyperLinkField HeaderText="Nomer Billing" Target="_blank" DataNavigateUrlFields="BillingNo" DataTextField="BillingNo" DataNavigateUrlFormatString="~/detailBillingTek.aspx?BillingNo={0}" />
        </Columns>
    </asp:GridView>



Code Behind :
C#
SqlConnection tarikdatakondisi1 = new SqlConnection(StartKoneksiDatatable());

            // buka koneksi kedalam datatable
            tarikdatakondisi1.Open();

            SqlDataAdapter sda1 = new SqlDataAdapter("select BillingNo, deskripsi, customername, remarks from mastertablebilling where tablestatusId = 1 and billingstatusId = 1", tarikdatakondisi1);
            DataTable dt1 = new DataTable();
            sda1.Fill(dt1);
            
            // lempar data tarikan ke row grid
            gridviewformindexteknis.DataSource = dt1;
            gridviewformindexteknis.DataBind();
            tarikdatakondisi1.Close();
 
Share this answer
 
Comments
haritz.aryandra 28-Oct-13 4:40am    
if that use, are the boundfield can show, because i used one hyperlink in gridview as the primary row column?
CodeBlack 28-Oct-13 4:51am    
hyperlink will remain as it is. what I have done is instead of code behind, I have bound all the columns in design page.
haritz.aryandra 28-Oct-13 5:12am    
oke, but how is in another condition, i want to add more bound to the grid, exp in radiobutton b condition, etc, do i must use condition in the asp.net? can i?
CodeBlack 28-Oct-13 5:16am    
yes you can.
haritz.aryandra 28-Oct-13 5:18am    
how? give me the code if the condition is, there is 3 radio button, 1 combobox and 1 text criteria. for exp, the previous is for radiobutton one checked, how about in radiobutton two checked etc??
i has solved the problem by my self, look's like the fault is because i assign the

Quote:
gridviewformindexteknis.Columns.Add(boundField);
gridviewformindexteknis.DataSource = null;

inside the looping condition, so maybe the system counting as the field is double value, because for the i++


i has updated the code behind also the asp.net :

this is the asp.net gridview of mine :

Quote:


<asp:Panel ID="panelgdformindeteknis" runat="server" style="overflow:auto" Width="100%">
<asp:GridView ID="gridviewformindexteknis" runat="server" BackColor="White"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3"
AllowPaging="false" OnPageIndexChanging="PageIndexChanging" style="overflow:auto" Width="100%" AutoGenerateColumns="False">
<footerstyle backcolor="#B5C7DE" forecolor="#4A3C8C">
<rowstyle backcolor="#E7E7FF" forecolor="#4A3C8C">
<pagerstyle backcolor="#E7E7FF" forecolor="#4A3C8C" horizontalalign="Right">
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<alternatingrowstyle backcolor="#F7F7F7">
<columns> <asp:HyperLinkField HeaderText="Nomer Billing" Target="_blank" DataNavigateUrlFields="BillingNo" DataTextField="BillingNo" DataNavigateUrlFormatString="~/detailBillingTek.aspx?BillingNo={0}" />
<asp:BoundField DataField="deskripsi" HeaderText="Deskripsi" />
<asp:BoundField DataField="customername" HeaderText="Customer" />




and this is the code behind :

Quote:
// kondisi radio button yang dipilih == dimana file billing status adalah new atau new updated(ini adalah status new yang pernah direject dan telah diupdated)
if (rboacceptedbilling.Checked == true)
{
if ((drpdpilihfungsi.SelectedValue == "Pilih") || (drpdpilihfungsi.SelectedValue == "") & (txtdeskripsi.Text == ""))
{
// assign boundfield sebagai array variable kolom space(ruang)
BoundField boundField5 = new BoundField();

SqlConnection koneksitarikdatadualaccepnofilt = new SqlConnection(StartKoneksiDatatable());

// buka koneksi kedalam datatable
koneksitarikdatadualaccepnofilt.Open();

SqlDataAdapter sda5 = new SqlDataAdapter("select BillingNo, deskripsi, customername, customerjabatan, remarks from mastertablebilling where tablestatusId = 1 and billingstatusId = 1 or billingstatusId = 3 or billingstatusId = 4", koneksitarikdatadualaccepnofilt);
DataTable dt5 = new DataTable();
sda5.Fill(dt5);

// masuk ke kondisi dimana datatable adakah atau tidak
if (dt5 != null)
{
if (dt5.Rows.Count > 0)
{
// melakukan perhitungan jumlah row kolom untuk dijadikan materi dan diinsert berapa banyak row kolom yang dibutuhkan dengan looping tiap field yang di select
for (int i = 0; i < dt5.Columns.Count; i++)
{
// buat kondisi jika ternyata ada field yang bernama tersebut, maka prosedural penetapan secara otomatis dilewatkan, karena sudah ada dari hardcode
if (dt5.Columns[i].ColumnName.ToString() != "BillingNo")
{
// kondisi sebaliknya jika ternyata ada field tersebut, maka tetapkan langsung headertextnya
boundField5.DataField = dt5.Columns[i].ColumnName.ToString();

if (dt5.Columns[i].ColumnName.ToString() == "deskripsi")
{
boundField5.HeaderText = "Deskripsi";
}
if (dt5.Columns[i].ColumnName.ToString() == "customername")
{
boundField5.HeaderText = "Customer";
}
if (dt5.Columns[i].ColumnName.ToString() == "customerjabatan")
{
boundField5.HeaderText = "Posisi";
}
if (dt5.Columns[i].ColumnName.ToString() == "remarks")
{
boundField5.HeaderText = "Remarks";
}
// asignment field yang sudah didapat hasil tangkapan ke boundfield untuk dijadikan row kolom sesuai dengan headertext yang sudah dibuat
//gridviewformindexteknis.Columns.Add(boundField5);
}
}
}
}
// asignment field yang sudah didapat hasil tangkapan ke boundfield untuk dijadikan row kolom sesuai dengan headertext yang sudah dibuat
gridviewformindexteknis.Columns.Add(boundField5);
gridviewformindexteknis.DataSource = null;// clear the grid

// lempar data array kedalam row gridview
gridviewformindexteknis.DataSource = dt5;
gridviewformindexteknis.DataBind();
koneksitarikdatadualaccepnofilt.Close();
}


the stack is shown, when i want to add more column as the filter nomer 2 searching(get data method), please anybody can give me the dynamic more code and script if there any of mistaken maybe by me
 
Share this answer
 
v2
Comments
haritz.aryandra 30-Oct-13 15:26pm    
in that case, why always the column "Posisi"{customerjabatan} not shown, always, eventhough the content i get is true, but that should to be have 5 column(add 1 row column more), this is not, only shown 4 column more and more???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900