Foros de CHW > Software > Lenguajes de Programación
Recuperar Clave
Registrarse FAQ/Ayuda Posts de hoy Tags


Estás leyendo el thread CREAR FILTRO EN GRIDVIEW (y pintar filas)  en el foro Lenguajes de Programación, que dice: "Saludos estimados, intenté buscar ayuda en el foro MSDN pero fue poco fructífero, tengo lo "

 




Tags: Ninguna

  Iniciar discusión  
 
LinkBack Herramientas
Antiguo 25-nov-2011, 09:17
[Black Vomit]
Deviloper
 
[Black Vomit]
 
Registrado: abril-2010
Posts: 84
[Black Vomit] no tiene una reputación buena ni mala


 
CREAR FILTRO EN GRIDVIEW (y pintar filas)

Saludos estimados, intenté buscar ayuda en el foro MSDN pero fue poco fructífero, tengo lo siguiente:
Quiero realizar un filtro en mi website que, según lo ingresado me pinte las filas de mi gridview, sean los caracteres que sean. Por ejemplo, ingreso "adm", le doy al botón BUSCAR y debería seleccionarme y pintarme las filas que contenga esas letras en cualquiera de sus campos.

Lo ideal es que se ejecute en el evento CLICK de mi botón buscar. Tengo un texbox en dónde se ingresa la búsqueda, el botoón y el gridview en dónde se pintarian las filas y me carga los datos de mi base de datos.

Cualquier ayuda sería bienvenida, estoy hace días con esto.!!

Lo estoy realizando en c# + sqlserver.
Acá el código de mi evento click del botoón BUSCAR.

protected void btn_IngFormSearch_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gv_ListarCalibres.Rows)
{

for (int i = 1; i <= 11; i++)
{
TextBox txt = row.FindControl(string.Format("TextBox{0}", i)) as TextBox;
//if ((txt != null) && (txt.Text == txt_IngFormBuscador.Text))
//e.row.BackColor = System.Drawing.Color.Red;
}

}
}

PD: Soy novato en c#, pero algo le aplico.
[Black Vomit] está fuera de línea   Citar y responder
Recuerda que no verás este banner una vez que te registres

Antiguo 25-nov-2011, 09:29
VaoS
Usuario
 
VaoS
 
Registrado: junio-2006
Ubicación: Santiago Centro
Posts: 331
VaoS no tiene una reputación buena ni mala


 
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)

yo lo que haría seria modificar el evento RowDataBound del Gridview, algo así:

Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && TextBox_Busqueda.Text != "")
        {
            for (int x = 0; x < e.Row.Cells.Count; x++)
            {
                string stock = e.Row.Cells[x].Text;
                if (stock.Contains(TextBox_Busqueda.Text))
                {
                    e.Row.Style.Add("background-color", "#C69D9D");
                }
            }

        }
    }
VaoS está fuera de línea   Citar y responder
Antiguo 25-nov-2011, 10:11
[Black Vomit]
Deviloper
 
[Black Vomit]
 
Registrado: abril-2010
Posts: 84
[Black Vomit] no tiene una reputación buena ni mala


 
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)

Quote:
Originalmente publicado por VaoS Ver post
yo lo que haría seria modificar el evento RowDataBound del Gridview, algo así:

Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && TextBox_Busqueda.Text != "")
        {
            for (int x = 0; x < e.Row.Cells.Count; x++)
            {
                string stock = e.Row.Cells[x].Text;
                if (stock.Contains(TextBox_Busqueda.Text))
                {
                    e.Row.Style.Add("background-color", "#C69D9D");
                }
            }

        }
    }
Gracias bro, pero mira lo que llevo actualmente...

protected void btn_IngFormSearch_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gv_ListarCalibres.Rows)
{

for (int i = 1; i <= 11; i++)
{
TextBox txt = row.FindControl(string.Format("TextBox{0}", i)) as TextBox;
if ((txt != null) && (txt.Text == txt_IngFormBuscador.Text))
{
row.BackColor = System.Drawing.Color.Red;
}
}

}
}
Se supone que creo una variable del tipo GRIDVIEWROW con la que voy iterando, luego, un bucle for que parte desde 1 hasta la cantidad de columnas de mi gridview, luego una variable del tipo TEXBOX la cuál será igual a los textbox de mis columnas plantillas con el formato "TextBox1, Textbox2".... etc, i si éstos están con algún dato y son iguales a lo que ingresé en mi txt_IngFormBuscador, debería pintarlos de rojo, ¿Verdad? pero no los PINTA!!!



Pero, basado en tú sugerencia, ¿Cómo podría unir el botón BUSCAR con ese evento? porque se supone que debería apretar BUSCAR y ahí debería de pintar las filas con los resultados...

---------- Post added at 09:58 ---------- Previous post was at 09:53 ----------

Porsiaca, tengo mis columnas como plantilla ... uffff estoy hace días con ésto

---------- Post added at 10:11 ---------- Previous post was at 09:58 ----------

//__________________________________________________ __________________
//----------------- FUNCION BUSCADOR /FILTRO POR CARACTER------------
protected void btn_IngFormSearch_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gv_ListarCalibres.Rows)
{
for (int i = 1; i <= 11; i++)
{
TextBox txt = row.FindControl(string.Format("TextBox{0}", i)) as TextBox;
if ((txt != null) && (txt.Text == txt_IngFormBuscador.Text))
{
row.BackColor = System.Drawing.Color.Red;
}
}

}
}

//__________________________________________________ __________________
//---------------- EVENTO ROWDATABOUND PINTAR FILAS ------------------
protected void gv_ListarCalibres_RowDataBound(object sender, GridViewRowEventArgs e)
{


if (e.Row.RowType == DataControlRowType.DataRow && txt_IngFormBuscador.Text != "")
{
for (int x = 0; x < e.Row.Cells.Count; x++)
{
string stock = e.Row.Cells[x].Text;
if (stock.Contains(txt_IngFormBuscador.Text))
{
e.Row.Style.Add("background-color", "#C69D9D");
}
}

}
}


Pero mi pregunta ahora es, ¿Cómo podría desde el evento click del botoón BUSCAR invocar al evento ROWDATABOUND que me pintaría mis filas?
[Black Vomit] está fuera de línea   Citar y responder
Antiguo 25-nov-2011, 10:25
VaoS
Usuario
 
VaoS
 
Registrado: junio-2006
Ubicación: Santiago Centro
Posts: 331
VaoS no tiene una reputación buena ni mala


 
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)

En el evento RowDataBound puedes ver que control desencadeno en postback (en este caso seria el boton buscar)

How to determine which Control caused PostBack on ASP.NET page?

Control que ha producido un evento PostBack | C#

Determining the Control that Caused a PostBack

Ahi filtras con un if si el control que hizo el postback es el botón buscar.

Yo creo que como tu lo planteaste tienes el problema del orden en que se ejecutan los eventos (LOAD, CLICK , DATABIND, etc) hay un flujo determinado pero no me acuerdo como era. O en una de esas simplemente te falta un gv_ListarCalibres.DataBind() al final para actualizar el gridview
VaoS está fuera de línea   Citar y responder
Antiguo 25-nov-2011, 11:23
[Black Vomit]
Deviloper
 
[Black Vomit]
 
Registrado: abril-2010
Posts: 84
[Black Vomit] no tiene una reputación buena ni mala


 
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)

Como quiero hacer un filtro, y de tanto averiguar, ¿no sería más factible instansiar un DATAVIEW y desde ahí filtrar?
¿Qué me recomiendas? así me ahorraría la paja del RowDataBound, y mostraría los que coincida con el filtro en vez de pintarlos, al fin y al cabo, la misma funcionalidad pero menos bonita.

¿Qué opinas?.
[Black Vomit] está fuera de línea   Citar y responder
Antiguo 25-nov-2011, 11:48
VaoS
Usuario
 
VaoS
 
Registrado: junio-2006
Ubicación: Santiago Centro
Posts: 331
VaoS no tiene una reputación buena ni mala


 
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)

Quote:
Originalmente publicado por [Black Vomit] Ver post
Como quiero hacer un filtro, y de tanto averiguar, ¿no sería más factible instansiar un DATAVIEW y desde ahí filtrar?
¿Qué me recomiendas? así me ahorraría la paja del RowDataBound, y mostraría los que coincida con el filtro en vez de pintarlos, al fin y al cabo, la misma funcionalidad pero menos bonita.

¿Qué opinas?.
jaja eso es lo que hubiera hecho desde el principio, yo pensé que era necesario que se pintaran las filas.
VaoS está fuera de línea   Citar y responder

  Foros de CHW > Software > Lenguajes de Programación

Herramientas

Reglas del Foro
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is activado
Emotíconos está activado
El código [IMG] está activado
El código HTML está desactivado
Trackbacks are activado
Pingbacks are activado
Refbacks are activado

Ir a


Estilo del foro: Todas las horas son GMT -3. La hora es 04:15.

Contacto Foro - Privacidad - FAQ - Reglamento Convivencia - Reglamento Compraventa - Ir arriba